Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
This question was closed May 08, 2016 at 02:56 PM by DJJD for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by DJJD · May 08, 2016 at 06:35 AM · objectnullreferenceexceptionreferenceinstancenull reference exception

NullReferenceException: Object reference not set to an instance of an object ?

Hi, I'm kind of new with Unity and I'm having difficulties with a single option when creating a GUI menu. What I want to do, is that when the pause menu appears, the player and the enemies stop moving. So i wrote this in my SettingPopup class :

  public GameObject menu;
     private RayShooter cursorMenu;
     void Start (){
     cursorMenu = menu.GetComponent<RayShooter> ();
 }

 public void Open() {
         gameObject.SetActive (true);
         cursorMenu.openMenu = true;
         Debug.Log ("OK3");
         Time.timeScale = 0f;
 }
 public void Close() {
         gameObject.SetActive (false);
         cursorMenu.openMenu = false;
         Time.timeScale = 1f;
     }
 }

And in my RayShooter class :

 public bool openMenu = false;
 
 void Update() {
         if (Input.GetKeyDown (KeyCode.T) && (!openMenu)) {
             ToggleCursorLock ();
         } 

shortly, ToggleCursorLock (); is starting a fonction stoping the player to move.

After testing everything out, when I enter my menu, the player still is available to move freely, same for the enemies. But, if I close the menu and start it back, everything's working exactly like I want. It's just the first time I got nothing at all. In the console, the first time, when it tries to read the cursorMenu.openMenu = false;, it stops running and I don't get the Debug.Log (wich appears the other times i open the menu). When I start the game I receive this error :

NullReferenceException: Object reference not set to an instance of an object

( The line cursorMenu.openMenu = false;)

SettingsPopup.Close () (at Assets/Scripts/SettingsPopup.cs:47)

(SettingsPopup.Close (); to start the game having the menu closed)

UiController.Start () (at Assets/Scripts/UiController.cs:25)

When I open the menu (The 1st time only) I get a similar error with the line

SettingsPopup.Open () (at Assets/Scripts/SettingsPopup.cs:40) and long long error lines that I don't get.

I don't understand what did wrong, what I need to do, and why it's working all the time except the first time I'm opening the menu. Help will be really appreciated :)

Thanks a lot

Joey

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 flashframe · May 08, 2016 at 01:35 PM 0
Share

Can you post the entire script? Is there a RayShooter component attached to your menu gameobject?

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Glurth · May 08, 2016 at 01:18 PM

I don't actually see the lines that generated the error you posted, in the code you posted, like "SettingsPopup.Close ()"

I can still point out that, this error is usually due to an object not being initialzized, in particular, instantiated. I see this line in your Start function:

 cursorMenu = menu.GetComponent<RayShooter> ();

However, due to the error, I suspect this is not being called initially, so cursorMenu is not instantiated before being used. I'm not quite sure why it's not being called though. Perhaps try using "Awake" instead of "Start"? That's usually where I would do initialization of that kind.

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 DJJD · May 08, 2016 at 02:52 PM 0
Share

Creating an "Awake" just for this line made it work exactly like I wanted, thanks. In fact, the moment I start the game, it closed my menu before having the "Start" working, and then he was looking for a disabled object. Having it in Awake makes it used before being disable, so thanks a lot :D

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

The infamous: Object Reference not set to an instance of an object 1 Answer

" NullReferenceException: Object reference not set to an instance of an object" 3 Answers

Object Reference Not Set to an Instance of an object 0 Answers

"Object reference not set to an instance of an object" 2 Answers

Object reference not set to an instance of an object 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