If statement 如何测试什么材质应用于统一的对象

If statement 如何测试什么材质应用于统一的对象,if-statement,unity3d,If Statement,Unity3d,我有一个钻石精灵,我想能够改变颜色的钻石从白色,它的原始颜色为绿色。然而,我不知道如何做到这一点 public class MoveControl : MonoBehaviour { // Update is called once per frame void Update () { if (Input.GetKey(KeyCode.A) ) { if (GetComponent<Renderer>().material.co

我有一个钻石精灵,我想能够改变颜色的钻石从白色,它的原始颜色为绿色。然而,我不知道如何做到这一点

    public class MoveControl : MonoBehaviour {
    // Update is called once per frame
    void Update () {
    if (Input.GetKey(KeyCode.A) )
    {
        if (GetComponent<Renderer>().material.color == Color.white)
        {
            GetComponent<Renderer>().material.color = Color.green;
        }  
    }

  }

}
公共类移动控件:单行为{
//每帧调用一次更新
无效更新(){
if(Input.GetKey(KeyCode.A))
{
if(GetComponent().material.color==color.white)
{
GetComponent().material.color=color.green;
}  
}
}
}
上面的代码就是我现在拥有的代码,它只在应用于精灵的材质(白色)是精灵/默认着色器时有效。这听起来可能不是什么大问题,但每当我应用不同颜色的不同材质(如蓝色)并更改其设置以使其具有精灵/默认着色器时,精灵将不可见


我是Unity的新手,如果有人能帮助我,我将不胜感激。我不知道你想做什么,但这可能会对你有所帮助

public class Material : MonoBehaviour {

Renderer renderer;
Color currentColor;
Color originalColor;

// Use this for initialization
void Start () {

    renderer = gameObject.GetComponent<Renderer>(); //If you don't know the gameObject is the object this script is attached to
    originalColor = renderer.material.color; //The original color ( in your case white )

}

// Update is called once per frame
void Update () {


    if (Input.GetKey(KeyCode.A)) {

        if (renderer.material.color == originalColor) { //Here we are checking if the color on the object is the original color
            renderer.material.color = Color.blue;   
            currentColor = renderer.material.color; //Here we are assining the current color
        }

    }

}
公共类材料:单一行为{
渲染器渲染器;
颜色;
原色;
//用于初始化
无效开始(){
renderer=gameObject.GetComponent();//如果您不知道gameObject就是此脚本附加到的对象
originalColor=renderer.material.color;//原始颜色(在本例中为白色)
}
//每帧调用一次更新
无效更新(){
if(Input.GetKey(KeyCode.A)){
如果(renderer.material.color==originalColor){//这里我们检查对象上的颜色是否为原始颜色
renderer.material.color=color.blue;
currentColor=renderer.material.color;//我们在这里分配当前颜色
}
}
}
}


我在编辑器中创建了一个材质并将其分配给游戏对象。

我不确定您想做什么,但这可能会对您有所帮助

public class Material : MonoBehaviour {

Renderer renderer;
Color currentColor;
Color originalColor;

// Use this for initialization
void Start () {

    renderer = gameObject.GetComponent<Renderer>(); //If you don't know the gameObject is the object this script is attached to
    originalColor = renderer.material.color; //The original color ( in your case white )

}

// Update is called once per frame
void Update () {


    if (Input.GetKey(KeyCode.A)) {

        if (renderer.material.color == originalColor) { //Here we are checking if the color on the object is the original color
            renderer.material.color = Color.blue;   
            currentColor = renderer.material.color; //Here we are assining the current color
        }

    }

}
公共类材料:单一行为{
渲染器渲染器;
颜色;
原色;
//用于初始化
无效开始(){
renderer=gameObject.GetComponent();//如果您不知道gameObject就是此脚本附加到的对象
originalColor=renderer.material.color;//原始颜色(在本例中为白色)
}
//每帧调用一次更新
无效更新(){
if(Input.GetKey(KeyCode.A)){
如果(renderer.material.color==originalColor){//这里我们检查对象上的颜色是否为原始颜色
renderer.material.color=color.blue;
currentColor=renderer.material.color;//我们在这里分配当前颜色
}
}
}
}

我在编辑器中创建了一个材质并将其分配给游戏对象。

在控制台中,它显示“此行为的引用脚本丢失!”。它未按预期工作。@NathanielOriecuia在控制台中尝试此操作,它表示“此行为的引用脚本丢失!”。它没有按预期工作。@NathanielOriecuia试试这个