Search code examples
unity-game-enginenetwork-programmingnullreferenceexceptiongameobject

Unity Fishnet OnStopClient - MissingReferenceException only sometimes?


Okay so I am using fishnets OnStartClient and OnStopClient methods. When I start the client I find a GameObject and when I stop it i'm trying to access the gameObject that was found onStart.

So I tested it out. When I stop and start the client 3 times, it works on the third time (Everytime). The first two tries:

  • I get "MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object."

To me it makes absolutely no sense why it works on the third run but not the first two. Does anyone have any input on this?

public override void OnStopClient()
    {
        base.OnStopClient();
        

            
        // This is what i'm getting nullReferenceException on but only sometimes.
        // I find thee invPanel in the onclient start using
        // invPanel = GameObject.FindGameObjectWithTag("InventoryPanel");
        if (!invPanel.activeSelf)
        {
            invPanel.SetActive(true);
        }

        
        
    }

I have tried everything I can think of along with researching the issue, but wasn't too successful on finding anything relating to my issue.

I was expecting it to either always find the object or always produce null reference exception. Makes no sense for it to work exactly on the third try everytime but not the first two tries.


Solution

  • So still not sure what the issue was but I believe I fixed it but just using OnApplicationQuit instead of OnStopClient.