1. Hello. I have a problem with a monobehaviour class. Here's the code I use to add it on a player gameObject :

    Code:
                BasePlayer pl = BasePlayer.activePlayerList[0];
                EquityPlayer epl = pl.gameObject.GetComponent<EquityPlayer>();
                if (epl == null) {
                    epl = pl.gameObject.AddComponent<EquityPlayer>();
               }
    
    This code is called after OnServerInitialized().

    As you can see this code is supposed to add the component only if it doesnt exist. It works fine - once. If I reload the plugin (without restarting the server), it seems to add the component to the player again, so with multiple plugin reloads, components pile up on the player.

    No problem with a full server restart tough, so its not a big deal, but its not clean either. Any idea whats going on ?
     
  2. You need to destroy the component at some point. So in RadtownAninals I destroy it when the animal is killed or on unload
     
  3. The thing is the player is neither killed or destroyed. Its just the plgin that is reloaded AND there is a check to make sure the compoenent isnt already present as you can see in the code, yet it is added again.
     
  4. try
    Code:
    if (player.GetComponent<EquityPlayer>())
                        UnityEngine.Object.Destroy(player.GetComponent<EquityPlayer>());
    then add the new component
     
  5. Ill try that, but basically isnt that trying to fix an unexpected result of a check using "GetComponent", by adding anoter check using "GetComponent" ?
     
  6. Its checking for old component and destroying it. You should be destroying the component anyway on plugin unload or your just stacking them
    Code:
    void Unload()
            {            
                var components = UnityEngine.Object.FindObjectsOfType<EquityPlayer>();
                if (components!= null)
                    foreach (var comp in components)
                        UnityEngine.Object.Destroy(comp);           
            }
     
  7. Unload cleanup sounds like a good idea, ill do that.
     
  8. It seems cleanup at the plugin unload fixed the problem. However I wanted to figure out what happened so I did more tests :

    In my monobehavior class I added a regular Debug.Log based on FixedUpdate to figure out if it is active or not.

    So every 20 fixedUpdates, I get a "Im alive" message.

    Then I add "GameObject.FindObjectsOfType<EquityPlayer>().Length" to that Debug.Log, so I get "Im alive, 1" messages.

    Now if I reload the plugin (not the server) multiples times, I still get "Im alive, 1" messages, but A LOT of them, directly related to the number of times I reloaded the plugin.

    So GetComponent or FindObjectsOfType fail to find multiples instances of my component, BUT multiple components scripts are still all running.
    Something noteworthy is that "ghost components" use older version of their scripts (as seen by the not-updated Debug.Log message).

    I assumed that somehow the "component object" is actually destroyed, but its code is still called.

    I tried to have the component have a self-check :

    Code:
                    bool found = false;
                    EquityPlayer[] eps = GameObject.FindObjectsOfType<EquityPlayer>();
                    foreach (EquityPlayer ep in eps) {
                        if (ep == this) found = true;
                    }
                    Debug.Log("Am i legit ?? " + found + " tot " + eps.Length);
    
    So it checks in the list of all components "detected" and see if its part of it. I assumed since FindObjectsOfType only returned one component, "ghost components" would not find themselves in the list. However ... they all say that the only Component found is themselves, and therefore are all assumed legit.

    My conclusion is as follows :

    - My script defines and adds a EquityPlayer component on a gameObject
    - I reload the plugin
    - My script re-defines and re-adds a EquityPlayer component on a gameObject
    - Since the component has been "re-defined", old versions of the component are not detected by GetComponent / FindObject. So when the new component tries to find "EquityPlayer" it only finds itselfs using the updated definition/identity of the component, and when the old component try to find "EquityPlayer", they also only find themselves with the outdated definition/identity. Hence all components believe they're legit.

    Anyways its fixed now. Thanks k1lly0u.