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
No description
No description
14 Replies
Angius
Angius4w ago
Do you ever give pointerClick any value?
41phaNum3r1ca1
41phaNum3r1ca1OP4w ago
If I am not mistaken, it should be the gameobject that was clicked on Oh.
Angius
Angius4w ago
And where do you set that?
41phaNum3r1ca1
41phaNum3r1ca1OP4w ago
Here is the relevant definition under PointerEventData... Im going to be honest, I have no idea how any of this works
No description
41phaNum3r1ca1
41phaNum3r1ca1OP4w ago
I assumed this somehow would???
Angius
Angius4w ago
Well, sure, PointerEventData object has a pointerClick property
Angius
Angius4w ago
So this
No description
Angius
Angius4w ago
You creating a property with the same name doesn't just automagically link them, or whatever it was you expected to happen $helloworld
Angius
Angius4w ago
Here's some tutorials on C# basics
41phaNum3r1ca1
41phaNum3r1ca1OP4w ago
see my friend, Im way beyond this. Ive just got a hole somewhere that needs patching, Im not starting from scratch!
Angius
Angius4w ago
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
41phaNum3r1ca1
41phaNum3r1ca1OP4w ago
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 :)
Angius
Angius4w ago
The condescending suggestion would be to use the debugger, not debug by printing to the console But glad it worked out for you

Did you find this page helpful?