代码之家  ›  专栏  ›  技术社区  ›  MatissossGameDev

Camera.main上出现Unity2D NullReferenceException错误。ScreenToWorldPoint(输入.鼠标位置);复制

  •  -1
  • MatissossGameDev  · 技术社区  · 2 年前

    当我在Unity编辑器中构建/玩游戏并构建自己时,Unity在SelectScript.cs:19上不断向我显示NullReferenceException错误

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

    有人知道如何解决这个错误吗?

    下面我发送此文件的完整代码:

    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();
        }
    }
    

    我尝试将Vector2更改为Vector3,但没有成功。早些时候它起到了应有的作用,但现在不起作用了。可能是什么原因导致的,以及如何解决?

    1 回复  |  直到 2 年前
        1
  •  0
  •   hijinxbassist    2 年前

    很可能没有标记为“主摄像头”的摄像头。
    相机是这一行中唯一可以为空的东西。

    Main Camera

    确保场景摄影机标记为“MainCamera”,或者曝光 Camera 字段,并通过检查器手动指定场景摄影机。