sectionというクラスを定義して、
SectionPointerというクラスの中でsectionの配列を作り(RightSections)、
全てのセクションに値を代入するスクリプトを作成しました。
しかし、"NullReferenceException: Object reference not set to an instance of an object"
のようにエラーが出てしまいます。
RightSectionsが参照できていないようです。
配列の扱い方に誤りがあるのでしょうか。
わかる方がいらっしゃいましたら、お力を貸していただけると幸いです。
以下は、今回書いたスクリプトの抜粋です。
CreateRightRoadMesh()内の、
RightSections [i].direction = Points [i + 1] - Points [i];
でエラーが起きています。
C#
1public class section : MonoBehaviour 2{ 3 public Vector3 direction; 4 public Vector3 right; 5 public Vector3 left; 6} 7 8public class SectionPointer : MonoBehaviour { 9 10 public Vector3[] Points; 11 [SerializeField] float RoadLength; 12 [SerializeField] float LineLength; 13 [SerializeField] float RoofHight; 14 public section[] RightSections; 15 16 void Start(){ 17 if (Points == null || Points.Length < 2) 18 return; 19 CreateRightRoadMesh (); 20 } 21 22 void CreateRightRoadMesh(){ 23 24 RightSections = new section[Points.Length]; 25 26 for (int i = 0; i < Points.Length; i++) { 27 if (i == 0) { 28 RightSections [i].direction = Points [i + 1] - Points [i];//StartPoint 29 30 } else if (i == Points.Length - 1) { 31 RightSections [i].direction = Points [i] - Points [i - 1]; 32 } else { 33 RightSections [i].direction = Points [i + 1] - Points [i - 1]; 34 } 35 36 }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/08/05 19:38