3
\$\begingroup\$

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;

    [RequireComponent(typeof(BoxCollider2D))]
    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.

enter image description here

What is causing the code to throw errors?

\$\endgroup\$
1
  • \$\begingroup\$ When I manually check the box for RaceAudio and play the game the audio is coming correctly. But when I uncheck the box for RaceAudio and try to enable it programmatically with the lines here then I get the error. Remember that I have use the same process in a different file and works fine. \$\endgroup\$
    – Erik
    Dec 24, 2016 at 5:16

2 Answers 2

8
\$\begingroup\$

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.
\$\endgroup\$
1
  • \$\begingroup\$ Thank you @Eric. So this is what I did. I got the GameObject by tag and use the reference AudioSource to disable it. All this into the Awake. Then into the Start I enabled it and play it. I appreciate your help. \$\endgroup\$
    – Erik
    Dec 25, 2016 at 4:46
2
\$\begingroup\$

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).

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .