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 /
avatar image
2
Question by Omni_shambles · May 23, 2018 at 03:34 PM · assign-variablenull referencevariable-definitionmember variables

Unassigned Reference Exception although it is assigned

Hello, there is an "Unassigned Reference Exception" in my game and it drives me mad. Apparently there are already many questions asked about the exact same problem but no answer was helpful for me. Now I will try my luck and ask the question myself:

I try to instantiate a prefab and set the canvas as its parent. This is an excerpt of my code:

 using UnityEngine;
 
 public class Inventory : MonoBehaviour
 {
     [SerializeField] Canvas canvas;
     [SerializeField] GameObject somePrefab;
 
     void Start()
     {
         canvas = GameObject.Find("Canvas").GetComponent<Canvas>();
         print(canvas.gameObject.name);
     }
 
     void Update()
     {
         print(canvas.gameObject.name);
     }
 
     public void MoveOnCanvas()
     {
         GameObject hoverObject = (GameObject)Instantiate(somePrefab);
         hoverObject.transform.SetParent(canvas.gameObject.transform, true);
     }
 }


It results in this output: alt text

The Start() callback works fine, the canvas gets printed. Also Update() works as it should. The MoveOnCanvas() method gets called when I click on a button. When MoveOnCanvas() gets called it results in the error. The variable "canvas" is assigned, otherwise the print statements wouldn't work! It even highlights the referenced object in the Hierarchy if I click on the field in the Inspector: alt text

I can not find a solution to this problem. I can't even find the cause of it. Many questions regarding this problem mentioned there might be a second instance of the Component (in my case "Inventory") or it might be attached to the wrong GameObject. This is not the case. I implemented a static integer variable and incremented it on Start(): Start only gets called once. As long as I get it correctly, this means there only is 1 instance of my Inventory.

If I change MoveOnCanvas() to this, it works fine:

 public void MoveOnCanvas()
     {
         GameObject hoverObject = (GameObject)Instantiate(somePrefab);
         hoverObject.transform.SetParent(GameObject.Find("Canvas").transform, true);
     }

How is this possible? Why can a member variable of a class can be assigned to some functions (Start() and Update() but contain null to others (MoveOnCanvas())? Maybe someone of you has a hint. I would appreciate it.

I could upload the whole class code to a Gist if you think it helps.

errorreference.png (50.7 kB)
errorassigned.png (17.7 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
5

Answer by Harinezumi · May 23, 2018 at 03:59 PM

When you click your button, on what object does the button call MoveOnCanvas()? Maybe there is a second Inventory script that has not been initialized, or clicking the button creates an Inventory script in that moment, and so its Start() has not yet been called ( Start() only gets called just before the first Update() for the given script); and when you see the error, it has already been initialized.
But I'm just guessing, this really is a weird error.

Comment
Add comment · Show 2 · 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 Omni_shambles · May 23, 2018 at 05:29 PM 1
Share

Your guess was crucial for the problem solving! Thank you! The button component has a reference to the Inventory prefab and not to the initialized inventory. The inventory prefab does not have an assigned canvas variable since this happens at runtime. I haven't implemented a working solution yet but in theory this has to be the cause! Damn it! Sometimes you miss the forest for the trees...

avatar image Harinezumi Omni_shambles · May 23, 2018 at 08:10 PM 0
Share

Cool, I'm glad I could help! :)
I converted the comment into an answer, as it turned out to be more relevant than expected.

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

82 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

Related Questions

Random Null Reference Exception 2 Answers

Null reference from object imported through method 1 Answer

Setting public variables by dragging: diverse results 1 Answer

How to assign GameObject used in a Custom Class? 0 Answers

Variables to represent the edges of the screen 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