0
using UnityEngine;

public class UnitClick : MonoBehaviour
{
    private Camera myCam;

    public LayerMask clickable;
    public LayerMask ground;

    void Start()
    {
        myCam = Camera.main;
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            **Ray ray = myCam.ScreenPointToRay(Input.mousePosition);**
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit, Mathf.Infinity, clickable))
            {
                if (Input.GetKey(KeyCode.LeftShift))
                {
                    UnitSelections.Instance.ShiftClickSelect(hit.collider.gameObject);
                }
                else
                {
                    UnitSelections.Instance.ClickSelect(hit.collider.gameObject);
                }
            }
            else
            {
                if (!Input.GetKey(KeyCode.LeftShift))
                {
                    UnitSelections.Instance.DeselectAll();
                }
            }
        }
    }
}

I killed more than half a day to find the source of the problem and did not find it...

I tried to rewrite a little and change the cameras to public ones and changed the raycast, but nothing helped...

Morion
  • 10,495
  • 1
  • 24
  • 33
VacAge
  • 1
  • Could you share the Code where you call the `Update()` method? It seems like you're trying to call the method on an object that's not been initialized yet. – baltermia Mar 13 '23 at 12:24
  • I would guess your `myCam` member is null. Did you tag the camera in your scene as `MainCamera` ? – Bart Mar 13 '23 at 12:52
  • https://pastebin.com/U1mPsaQK – VacAge Mar 13 '23 at 13:30

0 Answers0