A
A
Armyashka2019-12-06 19:10:54
C++ / C#
Armyashka, 2019-12-06 19:10:54

Why doesn't the EventSystem.IsPointerOverGameObject function work for me?

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
public class Ray_Shoot : MonoBehaviour
{
    private Camera _camera;
    void Start()
    {

    }
    void Update()
    {
        if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
        {
            Vector3 point = new Vector3(_camera.pixelWidth / 2, _camera.pixelHeight / 2, 0);
            Ray ray = _camera.ScreenPointToRay(point);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                Reactive_Target target = hit.transform.gameObject.GetComponent<Reactive_Target>();
                if (target!=null)
                {
                    target.ReactToHit();
                } else
                {
                    StartCoroutine(SphereIndicator(hit.point));
                }
            }
        }
    }
    void OnGUI()
    {
        int size = 12;
        float posX = _camera.pixelWidth / 2 - size / 4;
        float posY = _camera.pixelHeight / 2 - size / 2;
        GUI.Label(new Rect(posX, posY, size, size), "*");
    }
    private IEnumerator SphereIndicator(Vector3 pos)
    {
        GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        sphere.transform.position = pos;
        yield return new WaitForSeconds(1);
        Destroy(sphere);
    }
}

Throws < Ray_Shoot.Update () (at Assets/#Scripts/Ray_Shoot.cs:15)>> error.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question