前提
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#
using System.Collections; using System.Collections.Generic; using UnityEngine; public class colorchange : MonoBehaviour { private float timeElapsed; public string myMessage; // Start is called before the first frame update void Start() { GameObject obj = (GameObject)Resources.Load("Cube"); GameObject instance = (GameObject)Instantiate(obj, new Vector3(1, 1, 1), Quaternion.identity); instance.GetComponent<Renderer>().material.color = new Color(Random.value, Random.value, Random.value); } // Update is called once per frame void Update() { timeElapsed += Time.deltaTime; float timeOut = 3.0f; if (timeElapsed > timeOut) { instance.GetComponent<Renderer>().material.color = new Color(Random.value, Random.value, Random.value); Debug.Log(myMessage); timeElapsed = 0; } } }
試したこと
”instance”の定義の仕方、定義の位置が悪いのだと思うのですがどうすればよいかわかりません。
補足情報(FW/ツールのバージョンなど)
Unity 3D 2020.3.31f1
まだ回答がついていません
会員登録して回答してみよう