The above line of code is odd, as it gives me a NullReferenceException error saying “Object reference not set to an instance of an object”. I have confirmed that there is a GameManager _gameManager, an object called WinTrophy and a script called GameManager.
Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.
This is the kind of mindset and thinking process you need to bring to this problem:
_GameManager = GameObject.Find("WinTrophy");
if (_GameManager == null) {
Debug.Log("It broke in part one");
} else {
_gameManager = _GameManager.GetComponent<GameManager>();
} if (_gameManager == null && _GameManager != null) {
Debug.Log("It broke in part two");
}
After getting the message “It broke in part one”, I was/am still confused. As I mentioned earlier, there are no spelling mistakes relating to objects and variables, and it was the first block of code to run in void Start(). I commonly use code like* _GameManager = GameObject.Find("WinTrophy"); in my scripts, so I don’t think there is an error that I couldn’t find.
*Only code that I change are the variables/object.
How about just finding the GameManager itself with FindObjectOfType() ? The name kinda implies there’s only one, correct?
This is another one of those classic reasons NOT to use GameObject.Find(), which must have been written a million times. For some unspecified reason it’s failing (yet again) above.
Or else just use a singleton:
Simple Singleton (UnitySingleton):
Some super-simple Singleton examples to take and modify:
Simple Unity3D Singleton (no predefined data):
Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:
These are pure-code solutions, do not put anything into any scene, just access it via .Instance!
If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:
public void DestroyThyself()
{
Destroy(gameObject);
Instance = null; // because destroy doesn't happen until end of frame
}
I can’t implement the FindObjectOfType because the game object I am calling is a prefab, and the scripting API says “::ref::.FindObjectOfType will return no Assets (meshes, textures, prefabs, …)”
I have already figured that out. Please read the entire thread. Currently my problem is that I need to find how to use FindObjectOfType to see if that fixes my problem.
well, can you please clarify what is NOT clear on it?
in the line
Camera cam = (Camera)FindObjectOfType(typeof(Camera))
all 3 “Camera”-s are the same, just representing the Camera class in unity. first to declare the cam variable, second time to cast the returned object and third to get the type. its all the same (and its really stupid to be honest, it shouldnt need the cast and the typeof but unity works in mysterious ways). If you click the red underlined word it actually takes you to the Camera class
Camera is mentioned 3 times, and cam is mentioned once. I have no clue what these are referring to, so I don’t know which relevant variables/names I should replace them with.
_gameManager = (GameManager)FindObjectOfType(typeof(GameManager));
if (_gameManager == null) {
Debug.Log("The game manager could not be found.");
}
Thanks, but after testing, I still have a problem. The above code is two excerpts of my script. After running the code, I got the message “The game manager could not be found.”, which indicates that _gameManager is null, which doesn’t fix my original question, why is this code broken? Also, the message still appeared after I commented some other code that was giving me an error, so nothing wrong there.
its not the code is broken, its the instantiation must have failed so the gamemanager does not exist or is inactive in the scene. otherwise this would have to find it… when you run the game, check in the editor hierarchy if the wintrophy is there, its active, and the script is attached to it.