Hello everyone,
I have an issue with my game code regarding object reference. I want that when the player clicks on a certain item, it will be added to a function in the first class. The class is used for selection management. This function recognizes the click action and the item itself.
For some reason when I call a function which belongs to the second class (Navigation bar class), Unity doesn’t recognize the Game Object which is got selected (The item). Hence, the function from the second class can’t be executed. I need help please.
The first class - ItemsSelectionManager
The second class - ItemPanel
The errors:
NullReferenceException: Object reference not set to an instance of an object
ItemsSelectionManager.Start () (at Assets/ItemsSelectionManager.cs:10)
NullReferenceException: Object reference not set to an instance of an object
ItemsSelectionManager.selectItem (UnityEngine.GameObject item) (at Assets/ItemsSelectionManager.cs:39)
ItemsSelectionManager.Update () (at Assets/ItemsSelectionManager.cs:27)
Thanks for everyone for every help!
If you have another way to code it generally let me know (Maybe the transition of GameObjects from One class to another one)
Fortunately it doesn’t even matter what you are doing. The answer is ALWAYS the same for NullReference.
How to fix a NullReferenceException error
https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/
Three steps to success:
- Identify what is null ← don’t move past this until you have got it right!
- Identify why it is null
- Fix that
For a future problem that actually needs a forum post, remember that photographs of code are not a thing. If you post a code snippet, ALWAYS USE CODE TAGS:
How to use code tags: Using code tags properly
Hi,
Thanks about the detailed response. I’ve really tried to solve the problem in order to understand the source of the problem after lots of attempts to change several variables without success.
I would be happy to get any concrete help for this problem regarding the object reference.
Thanks.
That doesn’t sound like step #1 to me!
Let’s recap:
Go read the text posted here: http://plbm.com/?p=221
If that doesn’t do it, go read general google things about how to read the message and figure out what the compiler is choking on. It’s nothing anyone here can do for you.
The complete error message contains everything you need to know to do Step #1 by yourself:
The important parts of the error message are:
- the file it occurred in (critical!)
- the line number and character position (the two numbers in parentheses)
- also possibly useful is the stack trace (all the lines of text in the lower console window)