NullReferenceException: Object reference not set to an instance of an objectPlayerContral.Update () (at Assets/PlayerContral.cs:31)というエラーがでてうまく処理が行われません。PlayerContralスクリプトでRouletteスクリプトからpublic bool値のroop1を読み取りそれをPlayerContralスクリプトのbool値roulettehanteiに代入しようとしているのですがPlayerContralの31行目のroulettehantei = roulette1.GetComponent<Roulette>().roop1;でエラーが起きているようです。まだまだ初心者ですので説明などが不十分な点がありますがどうかご教授お願い致します。
ちなみにこれはルーレットをまわしてその数を読み取り、出目の数だけプレイヤーが移動できるようなゲームを作ろうとしています。
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class PlayerContral : MonoBehaviour 6{ 7 public bool test = true; 8 int deme1; 9 bool roulettehantei;//Rouletteスクリプトのroop1の値を入れるためのもの 10 11 void Update() 12 { 13 14 GameObject director1 = GameObject.Find("GameDirector"); 15 GameObject roulette1 = GameObject.Find("Roulette"); 16 roulettehantei = roulette1.GetComponent<Roulette>().roop1;//ここでエラーがでる。 17 if (roulettehantei == true) 18 { 19 deme1 = director1.GetComponent<GameDirector>().deme; 20 roulettehantei = false; 21 } 22 23 if (deme1 <= 0) 24 { 25 roulettehantei = true; 26 } 27
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class Roulette : MonoBehaviour 7{ 8 float kaitenspeed = 0; 9 bool roop = false; 10 public bool roop1 = false;//この値をPlayerContralスクリプトで取得したい 11 12 13 void Start() 14 { 15 16 //this.roulette = GameObject.Find("ImageRoulette"); 17 18 } 19 void Roulette1()//画像のルーレットでさされた数を数字として取得するやつ 20 { 21 GameObject director = GameObject.Find("GameDirector"); 22 Quaternion quaternion = this.transform.rotation; 23 float z = quaternion.eulerAngles.z; 24 while(z > 342.5) 25 { 26 z -= 360; 27 } 28 29 if (-17.5 <= z && z < 19) 30 { 31 director.GetComponent<GameDirector>().RoulletRsulut(1); 32 } 33 if (19 <= z && z < 55) 34 { 35 director.GetComponent<GameDirector>().RoulletRsulut(2); 36 } 37 if (55 <= z && z < 91) 38 { 39 director.GetComponent<GameDirector>().RoulletRsulut(3); 40 } 41 if (91 <= z && z < 127) 42 { 43 director.GetComponent<GameDirector>().RoulletRsulut(4); 44 } 45 if (127 <= z && z < 162.8) 46 { 47 director.GetComponent<GameDirector>().RoulletRsulut(5); 48 } 49 if (162.8 <= z && z < 198.6) 50 { 51 director.GetComponent<GameDirector>().RoulletRsulut(6); 52 } 53 if (198.6 <= z && z < 234.6) 54 { 55 director.GetComponent<GameDirector>().RoulletRsulut(7); 56 } 57 if (234.6 <= z && z < 270.8) 58 { 59 director.GetComponent<GameDirector>().RoulletRsulut(8); 60 } 61 if (270.8 <= z && z < 306.8) 62 { 63 director.GetComponent<GameDirector>().RoulletRsulut(9); 64 } 65 if (306.8 <= z && z < 342.5) 66 { 67 director.GetComponent<GameDirector>().RoulletRsulut(10); 68 } 69 } 70 71 72 void Update() 73 { 74 if (Input.GetMouseButtonDown(0))//左クリックで回転する。 75 { 76 this.kaitenspeed = 100; 77 this.roop = false; 78 } 79 if (Input.GetMouseButtonUp(0)) 80 { 81 this.roop = true; 82 } 83 if (roop == true)//左クリックを離すと減速する。 84 { 85 this.kaitenspeed *= 0.90f; 86 if (this.kaitenspeed < 0.01) 87 { 88 this.kaitenspeed = 0; 89 roop = false;//回転を徐々にゆっくりなど制御するためのbool値 90 roop1 = true;//このroop1をPlayerContralスクリプトで取得したい。 91 } 92 } 93 transform.Rotate(0, 0, this.kaitenspeed);//ここで回転させている。 94 95 if (this.kaitenspeed < 0.01 && roop1 == true) 96 { 97 Roulette1();//ここで画像のルーレットの出目を読み取っている。回転のスピードが0.0001未満になったら数字を表示する。 98 99 } 100 101 102 103 } 104} 105
回答4件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/04 04:47
2019/08/04 05:16