๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Error

[unity] NullReferenceError

728x90

unityNullReferenceError

  • ์œ ๋‹ˆํ‹ฐ์—์„œ Raycast ์Šคํฌ๋ฆฝํŠธ ์ ์šฉ ์‹œ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectTouchEvent : MonoBehaviour
{
    // Update is called once per frame
    void Update()
    {
            ClickDetect();
    }

    private void ClickDetect()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Debug.Log("Click ButtonDown(0)");
            RaycastHit hit_info;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit_info, Mathf.Infinity))
            {
                Debug.Log("์™œ ์—ฌ๊ธฐ๋Š” ์•ˆ๋“ค์–ด์˜ค๋Š”๊ฐ€?");
                Debug.Log(hit_info.collider.gameObject.name);
            }
        }
    }
}

์œ ๋‹ˆํ‹ฐ ์‹คํ–‰ ํ›„

์Šคํฌ๋ฆฐ์ƒท 2021-08-01 ์˜คํ›„ 10 36 14

 

  • NullReferenceException: Object reference not set to an instance of an object

ํ•ด๊ฒฐ๋ฐฉ๋ฒ•

์Šคํฌ๋ฆฐ์ƒท 2021-08-01 ์˜คํ›„ 10 36 47

  • main Camera์— Tag๋ฅผ MainCamera๋กœ ์ˆ˜์ •ํ•œ๋‹ค.