Unityで数字順押しゲーム(タッチナンバー)を作る
(https://gamegame-game.com/archives/touch_number_1/)
を参考にして数字タッチゲームを作っています。
スクリプトをすべて書き終えましたが、
いざゲームを実行すると
①ボタンの数字がすべて「1」になる。
②OnClickがうまく動かない(NullReferenceException)
となってしまいます。
どの部分を修正するのかが分かりませんので、お教えください。
よろしくお願いします。
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class NumberButtonControllerScript : MonoBehaviour 7{ 8 public GameDirectorScript GameDirectorScript { get; set; } 9 public string Text { get; private set; } 10 public int Number { get; private set; } 11 12 public int Width = 40; 13 public int Heigh = 40; 14 15 16 // Start is called before the first frame update 17 void Start() 18 { 19 this.GetComponent<Button>().onClick.AddListener(OnClick); 20 this.GetComponent<RectTransform>().sizeDelta = new Vector2(this.Width, this.Heigh); 21 SetButtonInfos(1, "1"); 22 23 24 } 25 26 // Update is called once per frame 27 void Update() 28 { 29 30 } 31 32 public void SetButtonInfos(int number, string text) 33 { 34 this.Text =text; 35 this.Number = number; 36 // Debug.Log(number); 37 38 this.GetComponentsInChildren<Text>()[0].text = text; 39 } 40 41 private void OnClick() 42 { 43 if (this.GameDirectorScript.CheckNumber(this.Number)) 44 { 45 46 this.GameDirectorScript.ChangeNextValue(); 47 Destroy(gameObject); 48 } 49 } 50} 51
C#
1using System.Collections; 2using System.Collections.Generic; 3using System.Linq; 4using UnityEngine; 5 6 7public class NumberButtonGeneratorScript : MonoBehaviour 8{ 9 public GameObject NumberButtonPrefab; 10 11 12 13 14 // Start is called before the first frame update 15 void Start() 16 { 17 18 } 19 20 // Update is called once per frame 21 void Update() 22 { 23 24 } 25 26 public void GenerateNumberButtons(int rowCount, int colCount) 27 { 28 var canvas = GameObject.Find("Canvas"); 29 30 var numbers = Enumerable.Range(1, rowCount * colCount) 31 .OrderBy(i => System.Guid.NewGuid()).ToArray(); 32 33 Debug.Log(string.Join(",",numbers)); 34 35 var buttons = GameObject.FindGameObjectsWithTag("NumberButton"); 36 foreach (var item in buttons) Destroy(item); 37 38 float offsetCountX = (colCount - 1) / 2.0f; 39 float offsetCountY = (rowCount - 1) / 2.0f; 40 41 int index = 0; 42 43 for (int y = 0; y < rowCount; y++) 44 { 45 for (int x = 0; x < colCount; x++) 46 { 47 48 int number = numbers[index++]; 49 //Debug.Log(number); 50 51 var button = Instantiate(this.NumberButtonPrefab) as GameObject; 52 53 54 var controller = button.GetComponent<NumberButtonControllerScript>(); 55 controller.SetButtonInfos(number, number.ToString()); 56 57 58 button.transform.SetParent(canvas.GetComponent<RectTransform>()); 59 60 61 62 63 button.transform.localPosition = new Vector3( 64 controller.Width * x - controller.Width * offsetCountX, 65 controller.Heigh * y - controller.Heigh * offsetCountY, 66 0); 67 } 68 } 69 } 70 71 72 73 74} 75 76 77 78
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class GameDirectorScript : MonoBehaviour 6{ 7 public static int ButtonRowCount = 3; 8 public static int ButtonColCount = 3; 9 public static int ButtonAllCount = ButtonRowCount * ButtonColCount; 10 11 public int NextNumber { get; set; } = 1; 12 13 14 public bool CheckNumber(int number) 15 { 16 return number == this.NextNumber; 17 } 18 19 public void ChangeNextValue() 20 { 21 this.NextNumber++; 22 } 23 24 // Start is called before the first frame update 25 void Start() 26 { 27 28 GameObject.Find("NumberButtonGenerator") 29 .GetComponent<NumberButtonGeneratorScript>() 30 .GenerateNumberButtons(ButtonRowCount, ButtonColCount); 31 32 } 33 34 // Update is called once per frame 35 void Update() 36 { 37 38 } 39} 40
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/11/11 03:37 編集
2019/11/11 04:08
2019/11/11 04:20