@XisterONE

Почему ошибка NullReferenceException?

using UnityEngine;
using UnityEngine.EventSystems;

public class GameManager : MonoBehaviour
{
    [SerializeField]
    private Player player;

 
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
       ClickTarget();
    }

    private void ClickTarget()
    {
        if (Input.GetMouseButtonDown(0) && !EventSystem.current.IsPointerOverGameObject())
        {
            Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);

            RaycastHit2D hit = Physics2D.Raycast(mousePosition, Vector3.zero, Mathf.Infinity, 512);

            if (hit.collider != null)
            {
                if(hit.collider.CompareTag("Enemy"))
                {
                    player.MyTarget = hit.transform.GetChild(0);
                }
            }

            else
            {
                player.MyTarget = null;
            }

        }
    }
}

В чем здесь ошибка. Вот ошибка:
NullReferenceException: Object reference not set to an instance of an object
GameManager.ClickTarget () (at Assets/Scripts/GameManager.cs:24)
GameManager.Update () (at Assets/Scripts/GameManager.cs:19)
  • Вопрос задан
  • 127 просмотров
Пригласить эксперта
Ответы на вопрос 2
GavriKos
@GavriKos Куратор тега Unity
Включаем дебагер и смотрим, что в 24 строке у нас Null.
Скорее всего - EventSystem.current. ЕГо надо добавлять на сцену. Остальное - в мануале юинити.
Ответ написан
Комментировать
@Armyashka
Как его добавить в сцену?
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы