前提・実現したいこと
4つのいずれかのボタンを押すと、それらを消して、次の新たな4つのボタンを生成する仕組みを実現したいです。
発生している問題・エラーメッセージ
下のコードで、スタートの中でProduceFirst関数を呼び出して、プレハブから生成することはできました。これらのプレハブにはFirstのタグが付いており、OnclickによってVanish関数を呼び出し、消すことにも成功しました。しかし、ProduceSecondを呼び出した際にエラーメッセージが出てしまいます。
エラーメッセージ UnassignedReferenceException: The variable AttackButton of FirstCommand has not been assigned. You probably need to assign the AttackButton variable of the FirstCommand script in the inspector.
該当のソースコード
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class FirstCommand : MonoBehaviour
{
public GameObject canvas;//キャンバス public GameObject FightButton; public GameObject ConversationButton; public GameObject FeedButton; public GameObject EscapeButton; public GameObject AttackButton; public GameObject WeaponButton; public GameObject BackButton; public GameObject DefenceButton; // Start is called before the first frame update void Start() { ProduceFirst(); } // Update is called once per frame void Update() { } public void OnClick() { switch (transform.name) { case "Fight": Debug.Log("tatakau"); Vanish(); ProduceSecond(); break; case "Conversation": Debug.Log("kaiwa"); Vanish(); ProduceSecond(); break; case "Feed": Debug.Log("esa") Vanish(); ProduceSecond(); ; break; case "Escape": Debug.Log("逃げる"); Vanish(); ProduceSecond(); break; default: break; } } void Vanish() { GameObject[] Firsts = GameObject.FindGameObjectsWithTag("First"); foreach (GameObject First in Firsts) { Destroy(First); } } void ProduceFirst() GameObject prefab1 = (GameObject)Instantiate(FightButton); prefab1.name = "Fight"; prefab1.transform.SetParent(canvas.transform, false); GameObject prefab2 = (GameObject)Instantiate(ConversationButton); prefab2.name = "Conversation"; prefab2.transform.SetParent(canvas.transform, false); GameObject prefab3 = (GameObject)Instantiate(FeedButton); prefab3.name = "Feed"; prefab3.transform.SetParent(canvas.transform, false); GameObject prefab4 = (GameObject)Instantiate(EscapeButton); prefab4.name = "Escape"; prefab4.transform.SetParent(canvas.transform, false); } void ProduceSecond() { GameObject prefab5 = (GameObject)Instantiate(AttackButton);//この文章にエラーがでる prefab5.name = "Attack"; prefab5.transform.SetParent(canvas.transform, false); GameObject prefab6 = (GameObject)Instantiate(WeaponButton); prefab6.name = "Weapon"; prefab6.transform.SetParent(canvas.transform, false); GameObject prefab7 = (GameObject)Instantiate(DefenceButton); prefab7.name = "Defence"; prefab7.transform.SetParent(canvas.transform, false); GameObject prefab8 = (GameObject)Instantiate(BackButton); prefab8.name = "Back"; prefab8.transform.SetParent(canvas.transform, false); }
}
### 言語
Cシャープ
試したこと
下のコードで、スタートの中でProduceFirst関数を呼び出して、プレハブから生成することはできました。これらのプレハブにはFirstのタグが付いており、OnclickによってVanish関数を呼び出し、消すことにも成功しました。しかし、ProduceSecondを呼び出した際にエラーメッセージが出てしまいます。有効になっているスクリプトについてはInspectorでプレハブの指定は行っています。
補足情報(FW/ツールのバージョンなど)
unity2018.3.12.f1
あなたの回答
tips
プレビュー