提示コードの//////コメント内部の一行ですが以下のエラーが出ます。なぜ参照出来ないのでしょうか?
例外 [ NullReferenceException: Object reference not set to an instance of an object ]
cs
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Player : MonoBehaviour 6{ 7 //方向 8 public Vector3 vec; 9 10 11 // Start is called before the first frame update 12 void Start() 13 { 14 vec = new Vector3(0, 0, 0); 15 } 16 17 // Update is called once per frame 18 void Update() 19 { 20 Debug.Log(vec); 21 } 22 23} 24
cs
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Control : MonoBehaviour 6{ 7 //public GameObject obj; 8 private RaycastHit hit; 9 10 11 private Player player; 12 13 14 // Start is called before the first frame update 15 void Start() 16 { 17 player = GetComponent<Player>(); 18 19 } 20 21 22 23 24 // Update is called once per frame 25 void Update() 26 { 27 RaycastHit hit; 28 int distance = 1000; 29 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 30 Debug.DrawLine(ray.origin, ray.direction * 1000, Color.red); 31 32 33 if (Input.GetMouseButtonDown(0)) 34 { 35 if (Physics.Raycast(ray, out hit, distance)) 36 { 37/////////////////////////////////////////////////////////////////////////////////////// 38 player.vec = hit.point; 39/////////////////////////////////////////////////////////////////////////////////////// 40 } 41 } 42 43 } 44 45 //オブジェクトを配置する 46 void PutObject() 47 { 48 RaycastHit hit; 49 int distance = 1000; 50 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); 51 Debug.DrawLine(ray.origin, ray.direction * 1000, Color.red); 52 53 54 if (Input.GetMouseButtonDown(0)) 55 { 56 if (Physics.Raycast(ray, out hit, distance)) 57 { 58 Debug.Log(hit.point); 59 60 //Instantiate(obj, hit.point + new Vector3(0, obj.transform.localScale.y / 2, 0), obj.transform.rotation); 61 } 62 } 63 64 } 65 66 67} 68
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。