【发布时间】:2016-04-01 03:05:56
【问题描述】:
我想在 Unity 中使用单个脚本更改多个游戏对象的颜色。我有点迷失在如何做到这一点。我是 Unity 新手,这对我来说是某种基础培训。
Unity 版本:5.3.4
观察到的行为:
将相同的脚本添加到其他游戏对象中,并全部更改为相同的颜色
预期行为:
单独更改游戏对象的颜色
尝试过的事情清单:
使用 -FindGameObject-
尝试使用 -GameObject- 访问材质
两个都试了
在多个脚本中思考以达到我想要的结果
这是代码
C#:
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;
}
}
}
也许我做错了什么,正如我所说我是 Unity 的新手,我很乐意接受任何帮助,如果它不友好就更好了。
谢谢
【问题讨论】:
标签: c# colors gameobject