I Have 20 Prefabs Being Initiated At The Beginning Of The Game. The Prefab Has A Script Called “Biker.cs”, The Script Has 5 Private Gameobjects:
private GameObject pauseMenu;
private GameObject settingsMenu;
private GameObject videoMenu;
private GameObject audioMenu;
private GameObject quitMenu;
In “void Start () { }” I Initiate Each Gameobject With:
pauseMenu = GameObject.Find("PauseMenu");
settingsMenu = GameObject.Find("SettingsMenu");
videoMenu = GameObject.Find("VideoMenu");
audioMenu = GameObject.Find("AudioMenu");
quitMenu = GameObject.Find("QuitMenu");
I Then Use Each Gameobject In “void OnGUI () { }” Like So:
if (hovering == true && pauseMenu.activeInHierarchy == false && settingsMenu.activeInHierarchy == false && videoMenu.activeInHierarchy == false && audioMenu.activeInHierarchy == false && quitMenu.activeInHierarchy == false)
{
//If not paused display gui, if paused do nothing
if (pauseCounter == 1)
{
Vector2 mousePos = new Vector2(Event.current.mousePosition.x, Event.current.mousePosition.y);
//Creates a text box displaying "clickedBikeFitness"
GUI.Box(new Rect((mousePos.x + 20f), mousePos.y, 80, 20), "" + networkFitness);
}
}
At The Beginning Of The Game It Gives Me An Error In The Console Saying
“NullReferenceException: Object reference not set to an instance of an object
Biker.OnGUI () (at Assets/Scripts/NeuralNetwork/Biker.cs:167)”
I Have Used This Exact Method In Other Scripts To Check Whether Or Not These Gameobjects Are Active.
I Have Also Double Checked And Made Sure Each Gameobjects Name Is The Exact Same Name In-Game.
Question:
Why Does It Give Me This Error On This Script And Not On Others Which I Did The Same Thing For?
Notes:
I Have Used This Exact Method In Other Scripts To Check Whether Or Not These Gameobjects Are Active.
I Have Also Double Checked And Made Sure Each Gameobjects Name Is The Exact Same Name In-Game.
Each Gameobject Is Parented Under A “Canvas” Gameobject, But In The Other Scripts I Have Used This Method I Just Had To Specify The Name And Not The Path.
I Am Using Unity 2017.1.0f3 Personal Version
I Am Using Microsoft Visual Studios Community 2017 Version 15.3.2