• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by aidengaydos7 · Sep 02, 2017 at 04:01 PM · uiprefabcanvasprefab-instancegameobject.find

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aidengaydos7 · Sep 02, 2017 at 04:03 PM 0
Share

Sorry For The Bad Structuring Of The Question

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aidengaydos7 · Sep 02, 2017 at 04:13 PM 0
Share

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)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

126 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Canvas with GUI elements in prefab act strangely 0 Answers

Access and modify Text within a prefab 1 Answer

How to set the UI Text position to be relative to his "grandfather" prefab 0 Answers

Using raycast to enable canvas that is linked to a prefabs gameobject 0 Answers

Using prefab in UI buttons and/or canvas? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges