- Home /
Prefab Initiated At Beginning Of Game Returns Error When Script On Prefab Attempts To Find A Gameobject In Game
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
Answer by spidermancy612 · Sep 02, 2017 at 04:10 PM
Can you add what code you have on line 167 for us? It appears the issue is occurring there. Right now my best guess is that when you go to find those objects in your start method that one of them might not currently be in the scene which means that it can't find the object your looking for.
If You Would Like $$anonymous$$e To Post The Whole Script I Will (It Is Long), But For Now Her Is Your Answer:
Line 167 Is:
if (hovering == true && pause$$anonymous$$enu.activeInHierarchy == false && settings$$anonymous$$enu.activeInHierarchy == false && video$$anonymous$$enu.activeInHierarchy == false && audio$$anonymous$$enu.activeInHierarchy == false && quit$$anonymous$$enu.activeInHierarchy == false)