MissingReferenceException

Hey I always get the Error from the title, when i change the scene or stop running the play mode.
When the scene loads the enemy-GameObjects spawn in, from my player-GameObjects, those are in a List. (First code)
When I then destroy a enemy-GameObject, it spawns a player-GameObject and this activates new enemy-GameObjects. And so on.

The error always sends me to the 2nd script. But i think the bug is in the 1st script with the List.

Also when i build the game, everything works perfect

    public List<GameObject> EnemyArea = new List<GameObject>();

    private void Start()
    {
        EnemyArea.RemoveAll(x => x == null);
        foreach (GameObject go in EnemyArea)
        {
                go.SetActive(true);
        }
       
    }
[SerializeField] GameObject playerLand;

    GameObject ownLand;
    private void Awake()
    {
        ownLand = GetComponent<GameObject>();
    }


    private void OnDestroy()
    {
        if (ownLand == null)
        {
            playerLand.SetActive(true);
        }
    }

My guess is when you exit play mode, OnDestroy runs, thus playerLand is null at that time since you aren’t in play mode at that point. You can always test this by doing a null check on playerLand. Debug.Log is also your friend.

You can add Debug.log calls to your Start method and your OnDestroy and spit out some values.

Note the steps for null/missing reference errors

  1. Find out what is null
  2. Find out why it’s null
  3. Fix it.

The answer hasn’t changed since the last time you asked here in the year 2021:

Not only that, the answer will NEVER change. The answer will ALWAYS be the same.

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

Ok i Fixed it now Thank You. I now understand why you guys say i should use the 3 steps.
But i still got a question.

[SerializeField] GameObject playerLand;
    GameObject ownLand;

    private void Awake()
    {
        ownLand = this.gameObject;
        Debug.Log(ownLand == null);
        Debug.Log(playerLand == null);
    }

    private void OnDestroy()
    {
        Debug.Log(this.ownLand == null);
        Debug.Log(this.playerLand == null);
        if (playerLand == null)
            return;
        else if(playerLand != null)
            this.playerLand.SetActive(true);
    }

On Awake, playerLand is != null, but when i stop the game in the OnDestroy function playerLand is == null then, but why?

This is how the gameObject gets destroyed

if (collision.CompareTag("soldier"))
        {
            if (RoundManager.Instance.roundPerTurn > 0)
            {
                Destroy(transform.parent.gameObject);

Objects will be destroyed in an arbitrary order. I presume that playerLand got destroyed before the object that OnDestroy() is being invoked on.