- Home /
GameObject.Find/WithTag returns null
Ok guys so I'm having the following problem.
In the constructor of one of my scripts no matter what I do, the variable "player" is null, even though it shouldn't be.
private StateManager manager;
private GameObject player;
private PlayerController controller;
public OnRailsPlayState (StateManager managerRef) // Constructor
{
// Ensures that each State had a reference to the manager class // JS
// manager of class Statemanger now holds a reference to StateManager // JS
manager = managerRef;
if(Application.loadedLevelName != "OnRailsPrototypeFinal")
Application.LoadLevel("OnRailsPrototypeFinal");
player = GameObject.Find ("SpeedCharacter");
if(player != null)
controller = player.GetComponent<PlayerController>();
Debug.Log (player);
Debug.Log (controller);
Yes there is a GameObject in the scene named "SpeedCharacter" just as it is typed there. The only thing I can think of is that it has something to do with the States system I'm trying to implement.
The Game starts in a scene that has a GameObject called GameManager in it with my stateManger script attached to it. At that point it is in the BeginSttae.
You click a buton in this scene and it switches you to a new scene and (state, the OnRailsPlayState listed here in code). In this scene is where the game object "SpeedCharacter" is.
So it shouldn't be null.
Any help at all would be greatly appreciated.
Thank you.
I am a bit confused because this code fragment is not complete. Please show us the entire class as well as where this gets called from.
If this is a constructor, then this can't be a Unity component because they don't use constructors, so please show us the Unity component call that creates a new object of this type.
Normally they do have start methods, but I'm setting this up via State$$anonymous$$achine. $$anonymous$$eaning, I have an interface that has the following in it.
namespace Assets.Code.Interfaces
{
public interface IStateBase
{
void StateUpdate();
void ShowIt();
}
}
I then have a Stat$$anonymous$$anager that throws control of its Update and OnGui methods to the StateUpdate and ShowIt methods that are found in every single state once a given state is active.
the constructors of each State serve as the start methods.
The above state is being called from the ShowIt method of my BeginSttate via the following code
public void ShowIt() {
const int buttonWidth = 84;
const int buttonHeight = 60;
// Draw a button to start the game
if (GUI.Button(new Rect(Screen.width / 3,(2 * Screen.height / 3) - (buttonHeight / 2),buttonWidth,buttonHeight),"OnRails"))
{
// On Click, load the first level.
// "Stage1" is the name of the first scene we created.
Application.LoadLevel("OnRailsPrototypeFinal");
Switch (1);
//Debug.Log (manager.activeState);
}
// Draw a button to start the game
// Center in X, 2/3 of the height in Y
if (GUI.Button(new Rect(Screen.width / 2 + buttonWidth,(2 * Screen.height / 3) - (buttonHeight / 2),buttonWidth,buttonHeight),"Defense"))
{
// On Click, load the first level.
// "Stage1" is the name of the first scene we created.
Application.LoadLevel("DefensePrototype");
Switch (2);
}
}
does that help clarify?
Answer by vyriance · Jun 05, 2014 at 12:32 AM
Hey guys I solved the problem. Basically I was switching states faster then the next scene was loading, thus the player object I declared in my script couldn't find the gameObject thus it was coming back as null.
Implemented a load screen to load between scene changes and now it works since it delays the swapping of states until the scene is loaded.
Your answer
![](https://koobas.hobune.stream/wayback/20220613150604im_/https://answers.unity.com/themes/thub/images/avi.jpg)