前提
Unity,C#どちらも始めたばかりです。
実現したいこと
Start()で"instance" (Cube)を生成し、Update()で”instance”をランダムな色にテンポよく変化させたいです。
発生している問題・エラーメッセージ
Assets\colorchange.cs(32,13): error CS0103: The name 'instance' does not exist in the current context
該当のソースコード
C#
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class colorchange : MonoBehaviour 6{ 7 private float timeElapsed; 8 public string myMessage; 9 10 // Start is called before the first frame update 11 void Start() 12 { 13 GameObject obj = (GameObject)Resources.Load("Cube"); 14 GameObject instance = (GameObject)Instantiate(obj, 15 new Vector3(1, 1, 1), 16 Quaternion.identity); 17 18 instance.GetComponent<Renderer>().material.color = new Color(Random.value, Random.value, Random.value); 19 20 } 21 22 // Update is called once per frame 23 void Update() 24 { 25 26 timeElapsed += Time.deltaTime; 27 28 float timeOut = 3.0f; 29 if (timeElapsed > timeOut) 30 { 31 instance.GetComponent<Renderer>().material.color = new Color(Random.value, Random.value, Random.value); 32 33 Debug.Log(myMessage); 34 timeElapsed = 0; 35 36 37 } 38 39 } 40} 41
試したこと
”instance”の定義の仕方、定義の位置が悪いのだと思うのですがどうすればよいかわかりません。
補足情報(FW/ツールのバージョンなど)
Unity 3D 2020.3.31f1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/05/10 02:59