Posted April 25, 2021 by CG BOSS Games
#ufo #unity3d #devlog
In unity when writing code we sometimes want to create a generic game object container to call onto the scene different objects graphics or images.
But sometimes if it cannot find the object it throws an error. If you look at the image this comes out as warning in the console tab.
The reason for this is when a game object such as a Star or Rock needs to access the scripts in the player you have to set that the Script Game Object exists.
Online I found some code samples that I used to perform a Null Check which is just a bunch of IF statements to see if the game object exist. This helps resolve the error and let the game run.
//Code
//Setup Communication with the Game Manager
GameManagerObj = GameObject.Find("GameManager");
if (GameManagerObj != null)
{
Debug.Log("Button Script Found Game Manager " + GameManagerObj.name);
GameManagerScript = GameManagerObj.GetComponent<gamemanager>();
}
else
{
Debug.Log("GameManager Not Found");
GameManagerObj = null;
GameManagerScript = null;
}