Is my Game Object There? Null Checker Reference Error
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;
}
UFO Unity
A Simple 2D Arcade shooter like asteroids. (In-Progress)
Status | Prototype |
Author | CG BOSS Games |
Genre | Educational |
Tags | 16-bit, 2D, Arcade, Casual, Pixel Art, Shoot 'Em Up, Singleplayer, Unity |
More posts
- 1.22 PrototypeMay 21, 2021
- WebGL PublishingApr 28, 2021
- HTML Game Published Dev BuildApr 25, 2021
- Speed to FastApr 21, 2021
- Game ManagerMar 23, 2021
- Devlog 1 - UFO UnityFeb 17, 2021
Leave a comment
Log in with itch.io to leave a comment.