【发布时间】:2017-02-13 21:57:11
【问题描述】:
我的 Assert 中有两个预制件;在我的场景中,我有两个预制件的实例。我试图通过单击按钮来更改一个预制件的所有实例的颜色,但我得到的是两个预制件的所有实例的颜色都发生了变化。如何指示预制件在特定功能内进行更改?我猜 gameObject 指的是我场景中的所有游戏对象,这可能就是所有实例改变颜色的原因。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class eventSensors : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
public void tempSensor() {
print("estas en la funcuion de tempSensor");
// this.gameObject.GetComponent<Renderer> ().material.color = Color.red;
//gameObject.GetComponent<Renderer>().sharedMaterial.color = Color.red;
var prefab= Instantiate(gameObject, transform.position, transform.rotation);
prefab.GetComponent<Renderer>().material.SetColor("_Color", Color.red);
}
public void lightSensor()
{
print("estas en la funcuion de lightSensor");
gameObject.GetComponent<Renderer>().sharedMaterial.color = Color.green;
}
//Sent to all game objects before the application is quit
//this is called when the user stops playmode.
private void OnApplicationQuit()
{
//reset all prefab color to default
gameObject.GetComponent<Renderer>().sharedMaterial.color = Color.white;
}
}
【问题讨论】:
-
是的,我的项目中只有一个材质文件夹
-
无需创建多个材料:请参阅我的答案。
-
建议:发布关于如何让 ti 发挥作用的问题作为对问题的回答。