Compiler problemSolved

I am adding a component to BasePlayer.

When the plugin is loaded using hot load everything works as it should. But if I reload the plugin with the "o.reload" command, the component is removed and not added.

 

namespace Oxide.Plugins
{
    [Info("ComponentTest", "Test", "1.0.0")]
    class ComponentTest : RustPlugin
    {
        class TestClass : MonoBehaviour {}

        [ChatCommand("ttt")]
        void TestCommand(BasePlayer player)
        {
            if (player.HasComponent<TestClass>()) Puts("Player has component.");
            else Puts("Player has no component!");
        }

        void OnServerInitialized()
        {
            foreach (BasePlayer player in BasePlayer.activePlayerList)
            {
                player.GetOrAddComponent<TestClass>();
            }
        }

        void Unload()
        {
            foreach (BasePlayer player in BasePlayer.activePlayerList)
            {
                TestClass component;
                if (player.TryGetComponent(out component)) UnityEngine.Object.Destroy(component);
            }
        }
    }
}

 

How to reproduce the problem:

1.I load the plugin using hot load (by saving it from the editor).

2.I enter the test command "ttt" and see a message in the console that the player has a component.

3.Then I reload the plugin with the command "o.reload".

4.I enter the command "ttt" again and see that the player has no component.

If I reload again with the command "o.reload", the component will appear.

 

MaybeI'm doing something wrong?

Answering myself. After some time I realized that instead of Destroy I should use DestroyImmediate. Because it will delete the component instantly, without waiting for the current frame to complete.

Locked automatically