Tried to use unitys eventsystem to set a tag to a clicked gameObject, getting nullReferenceException
Hey yall. Im trying to give the "selected" tag to my tile gameobjects when I click on them. The attached script is a child of the tiles. It is supposed to work as follows:
1. Find the previously selected tile.
2. Remove its selected tag(if no previously selected tile exists(this is the first tile that has been selected), skip this step)
3. Apply the selected tag to gameObject we just clicked on.
However, I get an error "Object Reference not set to an instance of an object" when I attempt to run the last line(step 3). Perhaps I am not using the event system properly?Any help will be greatly appreciated


14 Replies
Do you ever give
pointerClick
any value?If I am not mistaken, it should be the gameobject that was clicked on
Oh.
And where do you set that?
Here is the relevant definition under PointerEventData... Im going to be honest, I have no idea how any of this works

I assumed this somehow would???
Well, sure,
PointerEventData
object has a pointerClick
propertySo this

You creating a property with the same name doesn't just automagically link them, or whatever it was you expected to happen
$helloworld
Here's some tutorials on C# basics
see my friend, Im way beyond this. Ive just got a hole somewhere that needs patching, Im not starting from scratch!
Allow me to express my doubt
Anyway, since you're past the beginner stuff, you should know how instances and properties work
So you should have no issue fixing the code
Ohhhhh! I just got it. When I first learnt, I accepted that when you make references, such as public GameObject gameObject, GameObject was a variable type(no one ever explained classes to me). I just looked up what one was, and oh my good lord up above that makes sense
All of this stuff Ive been taught as "this is just how it is, itll make sense later" and then it didnt make sense later and I just accepted it
see, this is one of the holes that needed patching
I have figured this out myself, without the condescending suggestion to read up on Debug.Log :)
The condescending suggestion would be to use the debugger, not debug by printing to the console
But glad it worked out for you