Help with EventSystem IsPointerOverGameObject

I’m using Unity 2017.1.0f3 and I’m trying to block ui touches in a UI canvas from moving through to the game objects and came across the boredmormongames tutorial but am having trouble with the first example.

When I add the below code as a monobehaviour to the cubes in my scene I get the error:

NullReferenceException: Object reference not set to an instance of an object
MouseDetection.OnMouseUp () (at Assets/MouseDetection.cs:9)
UnityEngine.SendMouseEvents:smile:oSendMouseEvents(Int32)

I’ve researched and don’t see an explanation.

Also, is there an equivalent to isPointerOverGameObject for mobile or will this work with both mouse and mobile touch? Thank you.

using System.Collections;
using UnityEngine;
using UnityEngine.EventSystems;

public class MouseDetection : MonoBehaviour {

    bool isRed;
    public void OnMouseUp(){
        if(!EventSystem.current.IsPointerOverGameObject ()){
            isRed =! isRed;
            if(isRed){
                GetComponent<Renderer>().material.color=Color.red;
            }
            else{
                GetComponent<Renderer>().material.color=Color.white;

            }
        }
    }
}

do you have EventSystem in your scene hierarchy?

3233657--248309--upload_2017-9-26_12-6-36.png

1 Like

That was it. Thank you.