Search code examples
c#unity-game-engine

Unity2D NullReferenceException error on Camera.main.ScreenToWorldPoint(Input.mousePosition);


When i build/play game in Unity Editor and Build itself Unity keeps showing me NullReferenceException Error on SelectScript.cs:19

originPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);

Someone know how to solve this error?

Below i send full code of this file:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SelectScript : MonoBehaviour
{
    private Vector3 originPoint, endPoint;
    public Sprite selectAreaSprite;
    private GameObject area;
    private BoxCollider2D areaCollider;
    public List<GameObject> SelectedGameObjects;
    [SerializeField] private GameObject menu;
    private bool wasClicked;
    public Vector3 connection;
    private void Update()
    {
        if(Input.GetMouseButton(0) == false)
        {
            originPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        }
        if (Input.GetMouseButtonDown(0))
        {
            CreateSelectArea();
        }
        if (Input.GetMouseButton(0))
        {
            endPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            area.transform.position = (originPoint + endPoint) / 2;
            area.transform.localScale = new Vector3(originPoint.x - endPoint.x, originPoint.y - endPoint.y, 0);
        }
        else if (Input.GetMouseButtonUp(0))
        {
            GameObject areaToDestroy = GameObject.Find("SelectArea");
            if(areaToDestroy != null)
            {
                Destroy(areaToDestroy);
            } 
        }
        if (Input.GetMouseButtonDown(1))
        {
            menu.transform.position = Input.mousePosition + new Vector3(0, 0, 10);
            menu.SetActive(true);
        }
    }
    private void CreateSelectArea()
    {
        area = new GameObject();
        area.name = "SelectArea";
        area.transform.position = originPoint - endPoint;
        var spriteRenderer = area.AddComponent<SpriteRenderer>();
        spriteRenderer.sprite = selectAreaSprite;
        spriteRenderer.color = new Color(0, 0.8f, 0, 1f);
        spriteRenderer.sortingOrder = -2;
        areaCollider = area.AddComponent<BoxCollider2D>();
        areaCollider.isTrigger = true;
    }
    public void Move()
    {
        wasClicked = false;
        StartCoroutine("waitForMouseClick");
    }
    private IEnumerator waitForMouseClick()
    {
        yield return new WaitForEndOfFrame();
        wasClicked = Input.GetMouseButton(0);
        if (!wasClicked)
        {
            StartCoroutine("waitForMouseClick");
        }
        else
        {
            foreach(GameObject g in SelectedGameObjects)
            {
                g.GetComponent<CardScript>().isSelected = false;
                g.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 10));
                if(g.GetComponent<LineRenderer>() != null)
                {
                    g.GetComponent<LineRenderer>().SetPosition(0, transform.position);
                }
            }
            Unselect();
        }
    }
    public void AddConnection()
    {
        StartCoroutine("waitForConnectionChange");
    }
    private IEnumerator waitForConnectionChange()
    {
        yield return new WaitForEndOfFrame();
        foreach (GameObject g in SelectedGameObjects)
        {
            g.GetComponent<CardScript>().AddConnection(Camera.main.ScreenToWorldPoint(Input.mousePosition));
        }
        if (!Input.GetMouseButton(0))
        {
            StartCoroutine("waitForConnectionChange");
        }
        else if(Input.GetMouseButton(0))
        {
            var mousePos = new GameObject();
            mousePos.name = "MousePosForConnection";
            mousePos.transform.position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            mousePos.AddComponent<CircleCollider2D>();
            mousePos.GetComponent<CircleCollider2D>().radius = 0.1f;
            mousePos.GetComponent<CircleCollider2D>().isTrigger = true;
            Unselect();
        }
        else if(connection != null)
        {
            foreach (GameObject g in SelectedGameObjects)
            {
                g.GetComponent<CardScript>().AddConnection(connection);
            }
            connection = new Vector3(0,0,0);
            Unselect();
        }
    }
    public void DeleteConnection()
    {
        foreach(GameObject g in SelectedGameObjects)
        {
            g.GetComponent<CardScript>().DeleteConnection();
        }
    }
    public void Unselect()
    {
        foreach(GameObject g in SelectedGameObjects)
        {
            g.GetComponent<CardScript>().isSelected = false;
        }
        SelectedGameObjects.Clear();
    }
}

I tried changing Vector2 to Vector3, but it wasn't working. Earlier it worked as it should, but now it doesnt work. What could probably cause that and how to fix it?


Solution

  • There is most likely no camera tagged "MainCamera".
    The camera is the only thing that can be null in that line.

    Main Camera

    Make sure that your scene camera is tagged "MainCamera", or expose a Camera field in your script and assign the scene camera manually through the inspector.