[solved] EventSystem...IsPointerOverGameObject error

Hey Community,

I have a strange problem (guess its just strange for me, and it would be some silly newby mistake).

After I have tested some of my functions i wanted to go on with a new scene.
I attached all my scripts to new gameobjects and prefabs (maybe this is important for my error?).

One of my scripts is responsible for getting details from any gameObject i click on. A typical RaycastHit → and it worked about 2 weeks ago! Not i always get a "
NullReferenceException: Object reference not set to an instance of an object
IdController.Update () (at Assets/IdController.cs:41)" Error

When i comment the EventSystem Line out i get my RaycastHit, but also have the problem about the scanning through GUI.
I really dont know what is wrong.
So here’s the Code. I provide you with the whole thing cause im not sure if the problem really appears in line 41 as mentioned.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class IdController : MonoBehaviour
{

    private GameObject ChosenObject;
    public string ChosenObjName;

    public GameObject Canvas_PlotDetail;

    public Vector3 ChosenObjCoordinates = new Vector3(0, 0, 0);

    public Text Text_Plot_Owner = null;
    public Text Text_Plot_Buildingtype = null;

    private int Plot_Owner = 0;
    private int Plot_HouseType = 0;
    private int Plot_ForSale = 0;
    // Start is called before the first frame update
    void Start()
    {
        Canvas_PlotDetail = GameObject.Find("Canvas_PlotDetail");

        Text_Plot_Owner = GameObject.Find("Text_Plot_Owner").GetComponent<Text>();
        Text_Plot_Buildingtype = GameObject.Find("Text_Plot_Buildingtype").GetComponent<Text>();

        Canvas_PlotDetail.SetActive(false);

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            //block raycast if UI is in line
            if(EventSystem.current.IsPointerOverGameObject())
            {
                Debug.Log("Via UI geblockt");
            }
            //No interface? -> go on
            else
            {

                RaycastHit hit;
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //Raycast für identifizierung des angewählten Objektes
                if (Physics.Raycast(ray, out hit, 100.0f))
                {
                    ChosenObjName = hit.transform.name;
                    ChosenObject = GameObject.Find(ChosenObjName);


                    ChosenObjCoordinates = GameObject.Find(ChosenObjName).transform.position;
                    Debug.Log("---" + ChosenObjName + "---");


                    Plot_Owner = ChosenObject.GetComponent<PlotController_ID>().Plot_Owner; //Auslesen des Besitzers von Plot ID Script

                    Plot_HouseType = ChosenObject.GetComponent<PlotController_ID>().Plot_HouseType; //Auslese des Gebäudetypes
                    Plot_ForSale = ChosenObject.GetComponent<PlotController_ID>().Plot_ForSale; //Auslese ob zu Verkauf oder nicht
                    if (Plot_ForSale == 1) //Ausführen falls Grundstück zum Verkauf steht
                    {
                        Canvas_PlotDetail.SetActive(true);
                    }

                    //Feststellen des Grundstücksbesitzers
                    switch (Plot_Owner)
                    {
                        case (0):
                            Text_Plot_Owner.text = "Verwaltet durch das Rathaus";
                            break;
                        case (1):
                            Text_Plot_Owner.text = "Das Grundstück gehört Ihnen!";
                            break;
                        default:
                            Text_Plot_Owner.text = "Wir wissen nicht wem dieses Fleckchen Erde gehören soll -> Error!";
                            break;
                    }

                    //Feststellen des Gebäudetyps
                    switch (Plot_HouseType)
                    {
                        case (0):
                            Text_Plot_Buildingtype.text = "Nur ein Stück Grünland!";
                            break;
                        case (1):
                            Text_Plot_Buildingtype.text = "Hier steht ein Wohnhaus.";
                            break;
                        case (2):
                            Text_Plot_Buildingtype.text = "Hier steht (BEISPIEL) eine Bäckerei.";
                            break;
                        default:
                            Text_Plot_Buildingtype.text = "Unbebaubar. Keine Ahnung was sie an diesem Fleckchen Land interessiert -> ERROR!";
                            break;
                    }


                }

            }

        }
    }

}

Thanks in advantage o/

2 Likes

EventSystem.current seems to return null, thus the NullReferenceException. Make sure you have an EventSystem in the scene.

2 Likes

Hell thanks Peter i found the solution about 1minute ago in another thread.
I dont know why there was no EventySystem in my Hirarchy anymore >_<

But you’ve been pretty right - it was missing.

Thank you. Short answer but big impact :slight_smile:

3 Likes

THANK YOU!

1 Like