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 /
  • Help Room /
avatar image
1
Question by ffollett · Jan 16, 2017 at 06:17 PM · c#scripting problemuinullreferenceexceptiononclick

Null Reference Exception only in second method

I've had several Null Reference Exception errors during the development of this project, and I felt like I had a handle on why they were happening, but this one has me stumped. I have a prefab (called specialFind) that gets instantiated several times throughout the scene on load. It has the following script on it (note that I took out several variables to save on space here, but if my comments aren't clear, I can provide the full code):

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class SFdata : MonoBehaviour {
     //public variables that receive data at instantiation
     public int ID;  //there are several others
 
     CamMouseLook mouselook;
     public GameObject viewer;
     public Canvas viewerCanvas;
 
     //Record Viewer Fields declared
     private Text viewerID;  //again, several others exist
 
     void Start()
     {
         viewer = GameObject.Find("recordViewer");
         viewerCanvas = viewer.GetComponent<Canvas>();
         viewerCanvas.enabled = false;
         Debug.Log("viewer is disabled");
         mouselook = GameObject.Find("Main Camera").GetComponent<CamMouseLook>();
 
         //assign correct components to record viewer fields
         viewerID = viewer.transform.Find("Panel/Header").GetComponent<Text>();
         //again, there are others
     }
     void OnMouseDown()
     {
         //when object is clicked on, open the dataViewer window and show the relevant data
         mouselook.enabled = false;
         Cursor.lockState = CursorLockMode.None;
         Cursor.visible = true;
         viewerCanvas.enabled = true;
         Debug.Log("Viewer is enabled");
 
         //set values for viewer
         viewerID.text = "Special Find: " + ID.ToString();
         //several others        
     }
     public void closeRecordViewer()
     {
         SFdata SFdata = this;
         Cursor.lockState = CursorLockMode.Locked;
         Cursor.visible = false;
         //next line throws Null Reference Exception
         mouselook.enabled = true;
         viewerCanvas.enabled = false;
     }
 }

So right now, things work as intended for the first two steps. If you click on an instance of specialFind it will open the data viewer and display the proper data. The problem happens when I click the 'X' button I made (grandchild of viewerCanvas), which has an OnClick event that calls closeRecordViewer(). For some reason, Start() and OnMouseDown() can use the variables like mouselook and viewerCanvas, but closeRecordViewer() gets a NRE when trying to access them. I thought that, being inside the same class, all methods would have access to these variables, but maybe I'm wrong? Can anyone tell me why this is happening?

Also, if you've got any criticism about the structure of the code, I'd very much appreciate that as well. But I mostly just want to get this thing working.

Comment
Add comment · Show 2
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 Arkaid · Jan 17, 2017 at 06:57 AM 0
Share

Have you tried setting those variables on the inspector rather than using Find()?

avatar image petraszd · Jan 17, 2017 at 05:11 PM 0
Share

$$anonymous$$y best guess would be that you are calling closeRecordViewer before Start. But since I can not see how/when you are instantiating SFdata object and how/when you are calling closeRecordViewer, I can't say for sure.

So, make sure you are calling closeRecordViewer after Start was called already.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Glurth · Jan 17, 2017 at 05:20 PM

Sounds like you have two components, of the same class (SFdata), on two or more objects in the Scene. Since you declared the variables without the "static" modifier, each instance of this class will store it's own copy of these variables. This makes sense when your class is say.. a text field, and need to store a unique string. However, if you want ALL instances of your class to be able to access the SAME variable, you will need to define the variable as static.

e.g.

 public class SFdata : MonoBehaviour {
      //public variables that receive data at instantiation
      public int ID;  //looks like this one SHOULD be stored in each instance.
  
      static CamMouseLook mouselook;  //I suspect this is one you wan to make static, and share this one variable among all instances of SFData
      public GameObject viewer;  // not sure if you'd want this to be the same of all instances....
      static public Canvas viewerCanvas; // I'd guess this one should be static too.
      ....
      }

If you choose to NOT make them static, you will need to ensure that every instance has it's variables setup properly when created or enabled.

e.g.

 OnEnable()
 {
    if(!mouselook || !viewerCanvas)
         Debug.Log("Warning, not all variables setup on object: " + gameObject.name);
 }
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 ffollett · Jan 18, 2017 at 11:25 PM 0
Share

That's probably what was happening. After playing with it some more, I refactored and moved the closeRecordViewer() into my UI manager script, where it probably should've been all along, and this fixed the problem. But I'm glad to know what the issue was. Apparently I need to go read up on static variables and scope more. Thanks!

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.

Follow this Question

Answers Answers and Comments

321 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 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 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

Object reference not set to an instance of an object - Jumping Scripts (C#) 1 Answer

Static function not working in between screen 2 Answers

I get a "NullReferenceException" when trying to change the text of a UI text box. 1 Answer

How to send event to my canvas via script 0 Answers

Always pickup closest item? 0 Answers


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