[過去ログ] 【ゲームエンジン】Unity初心者質問スレBuild4 (1002レス)
1-

このスレッドは過去ログ倉庫に格納されています。
次スレ検索 歴削→次スレ 栞削→次スレ 過去ログメニュー
429
(2): 2020/05/12(火)18:57 ID:oz8b1+GC(4/5) AAS
void OnEnable()
{
GameObject _button1 = GameObject.Find("Canvas").transform.Find("button1").gameObject;
_button1.gameObject.SetActive(true);
GameObject _button2 = GameObject.Find("Canvas").transform.Find("button2").gameObject;
_button2.gameObject.SetActive(false);
}
↑このようなコードをスタート関数を用いる方法で書き直しました↓

GameObject _button1;
GameObject _button2;

void Start()
{
_button1 = GameObject.Find("Canvas").transform.Find("button1").gameObject;
_button2 = GameObject.Find("Canvas").transform.Find("button2").gameObject;
}

void OnEnable()
{
_button1.gameObject.SetActive(true);
_button2.gameObject.SetActive(false);
}

そうしたら_button1.gameObject.SetActive(true);の行の所で
NullReferenceException: Object reference not set to an instance of an object というエラーがでました。
どのオブジェクトにも参照を持たない参照変数にアクセスしようとするエラーらしいのですが、一応動作はしている見たいで、参照もできている感じですが無視していいのでしょうか。
スタート関数を用いないほうがいいのでしょうか。
1-
あと 573 レスあります
スレ情報 赤レス抽出 画像レス抽出 歴の未読スレ AAサムネイル

ぬこの手 ぬこTOP 0.009s