実現したいこと
unity3D上で動くロボットアームを作成しています。
sliderに合わせて動かしたいのですがエラー"Object reference not set to an instance of an object"が出てしまい困っています。
発生している問題・分からないこと
sliderを動かすことにより関節の角度を決めて、ロボットアームを動かしたい。
エラーメッセージ
error
1"Object reference not set to an instance of an object" 2 3 4### 該当のソースコード 5 6```C# 7using System.Collections; 8using System.Collections.Generic; 9using UnityEngine; 10using UnityEngine.UI; 11 12namespace InverseKinematics 13{ 14 public class JointController : MonoBehaviour 15 { 16 //robot 17 private GameObject[] Joint = new GameObject[2]; 18 private GameObject[] Arm = new GameObject[2]; 19 private float[] armL = new float[2]; 20 private Vector3[] angle = new Vector3[2]; 21 22 //UI 23 private Slider[] slider = new Slider[2]; 24 private Text[] angText = new Text[2]; 25 private Text[] posText = new Text[2]; 26 27 void Start() 28 { 29 //robot 30 for (int i = 0; i < Joint.Length; i++) 31 { 32 Joint[i] = GameObject.Find("Joint_" + i.ToString()); 33 Arm[i] = GameObject.Find("Arm_" + i.ToString()); 34 armL[i] = Arm[i].transform.localScale.x; 35 } 36 37 for (int i = 0; i < Joint.Length; i++) 38 { 39 slider[i] = GameObject.Find("Slider_" + i.ToString()).GetComponent<Slider>(); 40 angText[i] = GameObject.Find("Angle_" + i.ToString()).GetComponent<Text>(); 41 } 42 posText[0] = GameObject.Find("Pos_X").GetComponent<Text>(); 43 posText[1] = GameObject.Find("Pos_Y").GetComponent<Text>(); 44 45 } 46 47 void Update() 48 { 49 for (int i = 0; i < Joint.Length; i++) 50 { 51 angText[i].text = slider[i].value.ToString("f2"); 52 angle[i].z = slider[i].value; 53 Joint[i].transform.localEulerAngles = angle[i]; 54 } 55 56 float px = armL[0] * Mathf.Cos(angle[0].z * Mathf.Deg2Rad) + armL[1] * Mathf.Cos((angle[0].z + angle[1].z) * Mathf.Deg2Rad); 57 float py = armL[0] * Mathf.Sin(angle[0].z * Mathf.Deg2Rad) + armL[1] * Mathf.Sin((angle[0].z + angle[1].z) * Mathf.Deg2Rad); 58 59 posText[0].text = px.ToString("f2"); 60 posText[1].text = py.ToString("f2"); 61 } 62 } 63}
試したこと・調べたこと
上記の詳細・結果
スペルミスやゲームオブジェクトの名前が原因かと思い修正や変更をして試しましたが上手くいきませんでした。
補足
特になし

0 コメント