現状
生成前に参照しているのかゲーム開始直後のみNullReferenceExceptionが出てしまう
ゲームは普通にできる
試したこと
タグをつけてタグがあるときのみ操作できるようにしたがNullReferenceExceptionがでた
エラーコード
NullReferenceException: Object reference not set to an instance of an object
AlphabetGenerator.Update () (at Assets/Scripts/AlphabetGenerator.cs:60)
NullReferenceException: Object reference not set to an instance of an object
AlphabetGenerator.Update () (at Assets/Scripts/AlphabetGenerator.cs:69)*4
該当コード
alphabetgenerator
1public class AlphabetGenerator : MonoBehaviour 2{ 3 public GameObject[] alphabets; 4 public Camera mainCamera; 5 public static int alphabetNum = 0; 6 public bool isGameOver; 7 private GameObject genealpha; 8 public bool isGene; 9 public bool isFall; 10 private int count; 11 Rigidbody2D rigid2D; 12 GameObject gameController; 13 14 // Start is called before the first frame update 15 void Start() 16 { 17 Reset(); 18 rigid2D = GetComponent<Rigidbody2D>(); 19 //開始時にgamecontrollerを見つける 20 gameController = GameObject.FindWithTag("GameController"); 21 } 22 23 public void Reset() 24 { 25 alphabetNum = 0; 26 isGameOver = false; 27 Move.isMoves.Clear(); 28 StartCoroutine(StateReset()); 29 } 30 31 // Update is called once per frame 32 void Update() 33 { 34 if (isGameOver) 35 { 36 return; 37 } 38 if (MoveCheck(Move.isMoves)) 39 { 40 return; 41 } 42 if (!isGene) 43 { 44 if (rigid2D.velocity.magnitude == 0) 45 { 46 Invoke("GenerateAlphabet", 0.5f); 47 isGene = true; 48 } 49 return; 50 } 51 Vector2 v = new Vector2(mainCamera.ScreenToWorldPoint(Input.mousePosition).x, transform.position.y); 52 if (Input.GetMouseButtonUp(0)) 53 { 54 if (!RotateButton.onButtonDown) 55 { 56///////////////////////////////////////////////////////////////////////////////////// 57 60行目 genealpha.GetComponent<Rigidbody2D>().isKinematic = false; 58//////////////////////////////////////////////////////////////////////////////////// 59 alphabetNum++; 60 isFall = true; 61 } 62 RotateButton.onButtonDown = false; 63 } 64 if (Input.GetMouseButton(0)) 65 { 66/////////////////////////////////////////////////////////////////////////////////////// 67 69行目 genealpha.transform.position = v; 68///////////////////////////////////////////////////////////////////////////////////// 69 } 70 71 72 } 73 74 IEnumerator StateReset() 75 { 76 while (!isGameOver) 77 { 78 yield return new WaitUntil(() => isFall); 79 yield return new WaitForSeconds(0.1f); 80 isFall = false; 81 isGene = false; 82 } 83 } 84 85 public void GenerateAlphabet() 86 { 87 if (count == 0) 88 { 89 genealpha = Instantiate(alphabets[Random.Range(0, alphabets.Length)], transform.position, Quaternion.identity); 90 genealpha.GetComponent<Rigidbody2D>().isKinematic = true; 91 gameController.SendMessage("IncreaseScore"); 92 } 93 } 94 95 public void RotateAlphabet() 96 { 97 if (!isFall) 98 { 99 genealpha.transform.Rotate(0, 0, -30); 100 } 101 } 102 103 bool MoveCheck(List<Move.Moving> isMoves) 104 { 105 if (isMoves == null) 106 { 107 return false; 108 } 109 foreach (Move.Moving b in isMoves) 110 { 111 if (b.isMove) 112 { 113 isGene = true; 114 isFall = true; 115 return true; 116 } 117 } 118 return false; 119 } 120 121 public void OnTriggerStay2D(Collider2D collision) 122 { 123 if (collision.CompareTag("Alphabet") || collision.CompareTag("SetAlphabet")) 124 { 125 count = 1; 126 } 127 } 128 129 private void OnTriggerExit2D(Collider2D collision) 130 { 131 count = 0; 132 } 133 134 135 public bool IsGameOver() 136 { 137 return isGameOver; 138 } 139 public void ReDead() 140 { 141 //ぶつかったらフラグを立てる 142 isGameOver = true; 143 144 } 145} 146
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2020/02/08 18:56
退会済みユーザー
2020/02/09 06:12