I seem to have a problem trying to detect a click on a UI image.
On the image itself, I added an ‘EventTigger’, added the ‘PointerClick’ event type, attached my script but I keep getting “NullReferenceException: Object reference not set to an instance of an object”
I took a look on Google before coming to make a post and everything I found didn’t work. It seems no matter what I try, I keep getting null.
Am I missing something super obvious? Below is the code I am using to detect the click.
public void ToggleOption()
{
Debug.Log(EventSystem.current.currentSelectedGameObject.name);
}
As you can see, its very basic. I’ve also attached a screenshot of the image and the canvas so you can see the settings.
NullReferenceException means you are trying to use a variable that doesn’t have a valid value. In this case, I would guess that currentSelectedGameObject is probably null.
I’m not sure if EventTrigger is considered selectable. You might consider using a Button instead.
You should definitely add some code that checks whether currentSelectedGameObject is null before you try to access its name.
The ui object needs to be interactable like a button what @Antistone said. Only raycasting options on the image wont make them selectable, only blocking raycasts.