NullReferenceException: Coding beginning

Hello,

i’m working on my first Unity “game”.

In my coding I now got a problem. I get the Error message “NullReferenceException: Object reference not set to an instance of an object”.

Even after asking uncle google i could not solve my problem.

if (count == 1 && run == 0) {
            StartCoroutine (WallTime ());


           
// problem

            GameObject.FindWithTag ("door closed").SetActive (false);
            Debug.Log (" ");
            GameObject.FindWithTag ("door opened").SetActive (true);

//problem





            //Instantiate (wall2, new Vector3 (13.93f, -0.19f, 4.2f), Quaternion.identity);
            //Instantiate (wall2, new Vector3 (13.93f, -0.19f, -4.403705f), Quaternion.identity);

            //, new Quaternion (0, 180, 0, 0)

            run = 1;
        }

thx

What line is the error referencing? I’m forced to assume the issue is that the engine couldn’t find any objects with tags “door closed” or “door opened”, and so throws the error when the SetActive method is called.
But without the whole error, I cannot accurately say.

Null reference means ‘something before a dot is null’. Chances are FindWithTag isn’t finding the appropriate GameObject.

Given your use case and the fact that the Find methods generally only return active GameObjects, I would suggest doing things differently. Try simply exposing the variables to the inspector and drag and drop the references.

it means you didn’t set the tag on the gameobject. you are looking for tag names “door closed” and “door opened”

please verify you have the tag named the same. and assigned to the gameobject