GameObject with Audio File called with FindGameObjectWithTag returns “Object reference not set to an instance of an object”

GameObject with Audio File called with FindGameObjectWithTag returns “Object reference not set to an instance of an object”

I'm puzzled with this error because in a different file (see below), I'm doing exact same process to an object to enable/disable and works fine. But this new object throws the error, the object has a Script calling an audio file. 
Enabling manually the Object, and playing the game the audio is fine. I hit the check box to disable it and I get the error "Object reference ..."
This audio object is attached to another GameObject which is the character of the game.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

    public class carController : MonoBehaviour {

        public float carSpeed;
        public float maxPos = 4.17f;

        Vector3 position;

        public ParticleHit pr;
        public AudioManager cr;
        public Blast cb;

                audioObj = GameObject.FindGameObjectWithTag("policeAuTag");
                audioObj.SetActive (true);
                cr.carSound.Play ();

What am I doing wrong.?
I have closed and reopened unity but nothing. I have changed the tag name but same error.
I have tried also "FindWithTag" and doesn't work.

What is causing the code to throw errors?


Answer 1:

What is going on?

It is not FindGameObjectWithTag that’s returning that error. I guess what you are actually seeing is something like this:

NullReferenceException: Object reference not set to an instance of an object
carController.Start () (at Assets/carController.cs:line-number)

Where line-number is the line number for audioObj.SetActive (true); and this is the actual source of the error.

This kind of error (a NullReferenceException) for that specific line can only mean that audioObj == null.

How to fix it?

So why is audioObj assigned null by FindGameObjectWithTag? There is no documentation entry for that function but FindGameObjectsWithTag is close enough:

Returns a list of active GameObjects tagged tag.

Emphasis mine; so FindGameObjectWithTag returns null when no active game object with the specified tag can be found.

So don’t deactivate this game object from the editor. Do one of these instead:

  • Store a reference to the game object in carController.Start or carController.Awake and deactivate it immediately. Then use that reference to activate it.
  • Deactivate the relevant specific component or components on the game object and activate those.

Answer 2:

I recently had this problem where I would start a scene with disabled objects and be unable to access them or enable them later on. Start the scene with your object enabled but add gameObject.SetActive(false); to the end of your Start() method in the last script on your gameObject. Link.

In your case, adding gameObject.SetActive(false); to AudioManager script should do it, but if you have that on other objects and you want them to be active, you should make a new script.

I’m not sure why this happens but I think Unity just doesn’t bother loading inactive items at scene start (at least in the Editor).