単一のスクリプトを使用してUnity内の複数のゲームオブジェクトの色を変更したいと思います。私はちょっとそれをやる方法に迷っています。私はUnityの新人です。これは私にとって基本的な訓練の一種です。Unity 5で複数のゲームオブジェクトの色を変更する
Unityバージョン:5.3.4
観察された行動:
:は、他のゲームオブジェクトに同じスクリプトと同じ色にすべての変更
予想される動作を追加しました
ゲームオブジェクトの色を個別に変更する
試した事の一覧:-FindGameObject-
を使用して
が同時に
複数のスクリプトで考えるの両方を試してみました-GameObject-
を使用して材料をアクセスもしようとしました私が望む結果を達成するために
コード
C#の:私はUnityへの新たなんだ言ったように
using UnityEngine;
using System.Collections;
public class ChangeColor : MonoBehaviour
{
//If I change these variables to -GameObject-
//It blocks me to access the renderer
//Making the variables public doesn't work either
private Renderer cube;
private Renderer sphere;
void Start()
{
//Tried here the -FindGameObjectWithTag-
cube = GetComponent<Renderer>();
sphere = GetComponent<Renderer>();
}
void Update()
{
if(Input.GetKeyDown(KeyCode.A))
{
//Tried here the -FindGameObjectWithTag-
cube.material.color = Color.red;
}
if(Input.GetKeyDown(KeyCode.S))
{
//Tried here the -FindGameObjectWithTag-
sphere.material.color = Color.green;
}
}
}
は、たぶん私は、何か間違ったことをやって、それがnoobfriendly優れている場合、私は親切に、任意の助けを受け入れます。
ありがとうございました