Missing Reference Exception Help?

I’m having an issue where I’m re-spawning the player in a scene. What happens is that the player is destroyed and then re-instantiated. However, when the player tries to move by modifying its transform.position, I get the following error.

MissingReferenceException: The object of type ‘Player’ 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.

The object type Playeris a C# script that holds data about the player. The script that is accessing Player, along with the Player script are on the same object. So when the player is destroyed and re-instantiated, a new Player script is generated as it’s a part of the player prefab. I don’t understand why it would be trying to access the old Player script (because there’s nothing in the scene trying to access it). In my understanding it should be sending the transform updates to the new Player script. …any help?

Heads up… I’m not looking for suggestions on better ways to re-spawn players (i.e. object pooling). I just want to know what’s going on with this error.

is there anything else in the scene which has a reference to the player? an enemy?

the error should list the script which is causing the error and the line.

This is just a guess, but do you have a player in your scene to begin with, or are they instantiated the first time? It may be that your scene player has the reference but your prefab doesn’t.

If all else fails, you can brute-force it by adding a quick check before you access the player reference:

if (null == playerRef)
{
    playerRef = GetComponent<Player>();
}

To answer both questions, there’s nothing in the scene that interacts with the player, and the player is spawned at the start. The only things that interact with the Player script, are ON the player prefab itself. The error is coming from a script ON the player prefab. Here are the lines of code generating the error.

 private void ServerData(ConnectionService con, ref NetworkMessage msg)
    {
        msg.DecodeData();

        if(msg.subject == networkID)
        {
            if(msg.tag == TagIndex.MovePlayer)
            {
                transform.position = (Vector3Carrier)msg.data;
                return;
            }
            if(msg.tag == TagIndex.RotatePlayer)
            {
                transform.rotation = (QuaternionCarrier)msg.data;
                return;
            }
        }
    }

This code is INSIDE the Player script itself, and the error is on line 9.

Do you have any delegate events that this method is added to as a listener? I get the same error when I reload a scene and not remove listeners from my delegate events from the previous scene. My delegate events were tracking listeners from the previous instance of the same scene which we were no-longer viewing.

Yes. This is using DarkRift Networking. There is a delegate event that is fired whenever a message is sent to the server. All objects are aware of the message, but they only process it if conditions are met (i.e. the IF statements in the code). So since this script is ON the player GameObject itself, transform CAN’T be null…but it’s returning as a MissingReference.

Is this method, as a listener, removed from the delegate event then? Like when the scene changes or when the player is deleted.

No, but the event can’t fire on a GameObject that doesn’t exist.

Couldn’t possibly hurt to try removing this listener from the delegate event when the player is respawned, or in this case re-initialized. IMO that is the most likely cause, especially if the delegate event is static.