[SOLVED]The Case of the Mysteriously Disappearing Tags!

For the main scene of my app, I have a GameObject tagged “Player” that contains all the different meshes for the player’s first and third person views. The different meshes and their accompanying scripts are activated and deactivated through code when the user switches views. The code finds the scripts and meshes by starting out with a GameObject.FindWithTag(“Player”). If I load the scene in the editor and run it, everything works fine.

However, the scene is meant to be launched from another scene which contains the main menu for the app. When I pull up the menu and start the app, it loads the main scene fine, with one exception.

The GameObject tagged “Player”…

cannot be found!

A statement like:

playerController = GameObject.FindWithTag("Player").GetComponent(ThirdPersonController);

produces the error:

NullReferenceException: Object reference not set to an instance of an object

Again, this works fine if I load the main scene itself and run it, but not if I load the main scene from the menu scene.

Any ideas on what’s going on?

Are you assigning the value of your playerController variable in an Awake() or Start() function?

2 questions:

  1. how’d you get a video in your signature?

try

// This will return the game object named Hand in the scene.
hand = GameObject.Find("Hand");

taken from Gameobject.Find

that should work and you don’t need tags just the name! :slight_smile:

It’s an animated .gif, not a video. :slight_smile:

I tried GameObject.Find, and tried putting the assignment in the Awake function, but no dice. :frowning:

Edit: I have also tried explicitly setting the playerController variable through the inspector, and it still doesn’t work. This is really vexing.

Update: I had it try printing the value of the playerController variable after explicitly setting it in the inspector.

When the scene is run by itself, it prints “ThirdPersonController” and everything works fine.

When the scene is loaded via the menu scene, it prints “null” and the script doesn’t work.

Why is it doing this? The value is assigned in the inspector, but it’s returning “null.” That shouldn’t be happening, should it?

Mystery solved!

I had been working on a new version of the scene, and had forgotten to change build settings so that the menu scene loaded the new version. So, essentially it was trying to load the old version of the scene, and the scripts all referenced things that no longer existed.

If you excuse me, I’ll be outside kicking myself in the head.