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

1 messages · Page 1 of 1 (latest)

hollow junco
#

https://pastebin.com/QHcwZZBf

I am getting this error and I suspect its because 'slot' isn't defined in my script.

NullReferenceException: Object reference not set to an instance of an object
puzzlepiece.OnMouseUp () (at Assets/scripts/puzzlepiece.cs:59)
UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)

No crashes, game runs fine except for this.

How do I get the engine to recognize the 'slot' object so the code will run? Using a 2D collider on the slot, btw

hollow junco
#

what do you mean?

wispy tundra
#

The error is in puzzlepiece. I'm guessing Vivvy didn't bother scrolling through the pastebin and he didn't notice the class was in there.

#

If you check the stacktrace, you will notice the last step was in that script, and it's something about a mouse event.

#

What exactly is line 59 of puzzlepiece @hollow junco ?

#

I can't figure it out with the pastebin

hollow junco
#

Oh okay!

hollow junco
#

Now I'm not sure how to get the engine to recognize the slot position

#

It doesn't

turbid sentinel
wispy tundra
#

Then slot is null

#

Judging by the code, I see a private field for slot, but no assignment. You also didn't do this through the inspector because it's a private unserialized field

#

So my guess is you forgot to initialize it

#

For next time, you can tell by the stacktrace where it went wrong, and you can start debugging your issue. In your case slot was the only variable that could be null, so you could put Debug.Log(slot) above line 59 to check it.

#

If slot is able to be null, you should change your code to if (Vector2.Distance(transform.position, slot?.transform.position) < 2) (not the question mark). It will then short circuit and stop checking, and the if-statement will fail, of course.

hollow junco
hollow junco
#

I found the private slot field and assigned it, but the error still appears

#

Also added slot.GameObject instead of just 'slot'

wispy tundra
#

Is this old code, or did you make a mistake?

wispy tundra
hollow junco
#

the current ss says 'slot'

#

my bad

#

slot = GameObject.Find("slot");

#

Like this?

#

Cannot implicitly convert type 'UnityEngine.GameObject' to 'puzzleslot' [Assembly-CSharp]csharp(CS0029)

I get this error in return tho

wispy tundra
#

In this case, type GameObject that tries to become a puzzleslot

#

GameObject.Find returns a GameObject.
I assume you have a puzzleslot component on the GameObject that is retrieved.
You basically need to get that component from the GameObject, by doing slot = GameObject.Find("slot").GetComponent<puzzleslot>();

hollow junco
#

yep

wispy tundra
#

However, this is not a good idea. GameObject.Find is slow and both it and GetComponent can return null

hollow junco
#

i see

#

i didnt know that haha

#

now i know

wispy tundra
#

What happened to the assignment in the inspector? Why don't you use that?

#

There does not seem to be a need for that line

hollow junco
#

i tried

#

still getting the null error

wispy tundra
#

Share the code please

#

And the new error

#

So the code without the find and the null error

hollow junco
#

sure thing

wispy tundra
#

What is line 61 referring to?

hollow junco
#

if (Vector2.Distance(transform.position, slot.gameObject.transform.position) < 2)

I think its to do with slot not being a gameobject again

#

but im not 100% certain

wispy tundra
#

This code has the exact same problem, a private field that is unassigned

wispy tundra
hollow junco
#

huh weird, i assigned it in the inspector

#

i wonder whats causing it

wispy tundra
#

This is completely different code to, because this is the puzzlepiece class, and the picture shows a puzzleslot

#

So the thing that you assign is not the slot that throws the error

#

You seem to confuse yourself by your own code 😛

hollow junco
#

ohhh

#

let me try to assign it to the proper code

#

slot.gameObject.transform.position

This one still brings up the error when assigned 😦

#

It works

#

okay i just had to set the value 2 much higher

wispy tundra
#

Not sure what you mean

hollow junco
#

The float value attached to the code that compares the distances

#

turns out it was too small

wispy tundra
#

Right

#

Your issue is now fixed, right?