実現したいこと
初心者です。「UnityではじめるC#」という書籍の脱出ゲーム制作についてです。制作過程でなぜそうなるのかが分からない点があり質問しました。
前提
こちらの書籍の脱出ゲーム制作の過程で、色が変わるボタンが横に3つ並んだ”金庫”が出てきます。初期設定は左から緑・赤・青に設定されており、ボタンを押すとその色から「緑→赤→青→白」の順でループします。特定の色の組み合わせ(左から青・白・赤の順が正解)になれば金庫が開き、中からアイテム(ハンマー)が獲得できるというものです。
このボタンのフラグに配列が使用されているのですが、どのようにして実際のボタンに紐づいているのかが分かりません。配列は「private int[] buttonColor = new int[3];」となっています。なぜこの配列がbuttonColor[0]が左のボタンのフラグ、buttonColor[1]が真ん中のボタンのフラグ、buttonColor[2]が右のボタンのフラグという風に紐づいているのかが分かりません。どこの操作がそのようになっているのでしょうか?
それぞれのGameObjectにはUnityのエディタ上でhierarchyから割り当てを行っています。
初歩的な事で大変恐縮です。ご回答いただければ幸いです。
該当のソースコード
C#
1using UnityEngine; 2using UnityEngine.UI; 3 4public class GameManager : MonoBehaviour 5{ 6 //定数定義:ボタンカラー 7 public const int COLOR_GREEN = 0; //緑 8 public const int COLOR_RED = 1; //赤 9 public const int COLOR_BLUE = 2; //青 10 public const int COLOR_WHITE = 3; //白 11 12 public GameObject buttonHammer; //ボタン:ハンマー 13 14 public GameObject buttonMessage; //ボタン:メッセージ 15 public GameObject buttonMessageText; //メッセージテキスト 16 17 public GameObject[] buttonLamp = new GameObject[3]; //ボタン:金庫 18 19 public Sprite[] buttonPicture = new Sprite[4]; //ボタンの絵 20 21 private bool doesHaveHammer; //ハンマーを持っているか? 22 private int[] buttonColor = new int[3]; //金庫のボタン 23 24 // Start is called before the first frame update 25 void Start() 26 { 27 doesHaveHammer = false; //ハンマーは「持っていない」 28 29 buttonColor[0] = COLOR_GREEN; //ボタン1の色は「緑」 30 buttonColor[1] = COLOR_RED; //ボタン2の色は「赤」 31 buttonColor[2] = COLOR_BLUE; //ボタン3の色は「青」 32 } 33 34 //金庫のボタン1をタップ 35 public void PushButtonLamp1() 36 { 37 ChangeButtonColor(0); 38 } 39 40 //金庫のボタン2をタップ 41 public void PushButtonLamp2() 42 { 43 ChangeButtonColor(1); 44 } 45 46 //金庫のボタン3をタップ 47 public void PushButtonLamp3() 48 { 49 ChangeButtonColor(2); 50 } 51 52 //金庫のボタンの色を変更 53 void ChangeButtonColor(int buttonNo) 54 { 55 buttonColor[buttonNo]++; 56 57 //「白」のときにボタンを押したら「緑」に 58 if (buttonColor[buttonNo] > COLOR_WHITE) 59 { 60 buttonColor[buttonNo] = COLOR_GREEN; 61 } 62 63 //ボタンの画像を変更 64 buttonLamp[buttonNo].GetComponent<Image>().sprite = buttonPicture[buttonColor[buttonNo]]; 65 66 //ボタンの色順をチェック 67 if ((buttonColor[0] == COLOR_BLUE) && (buttonColor[1] == COLOR_WHITE) && (buttonColor[2] == COLOR_RED)) 68 { 69 //まだトンカチを持っていない? 70 if(doesHaveHammer == false) 71 { 72 DisplayMessage("金庫の中にトンカチが入っていた。"); 73 buttonHammer.SetActive(true); //ハンマーの絵を表示 74 75 doesHaveHammer = true; 76 } 77 } 78 } 79 80 //メッセージを表示 81 public void DisplayMessage(string mes) 82 { 83 buttonMessage.SetActive(true); 84 buttonMessageText.GetComponent<Text>().text = mes; 85 } 86} 87 88
試したこと
上記の内容で試して普通に動きます。ChangeButtonColorの引数を変えたりして試しています。該当の番号のボタンの色が変わります。なぜそうなるのかが分かりません。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2024/04/01 08:19
2024/04/01 11:01
2024/04/02 11:53
2024/04/03 09:41
2024/04/06 07:36