2016-08-02 64 views
1

我有一顆鑽石精靈,我希望能夠從白色改變鑽石的顏色,它的原始顏色爲綠色。但是,我無法弄清楚如何做到這一點。如何在Unity中測試應用於物體的材質

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; 
     } 
    } 

    } 

} 

上面的代碼就是我現在使用的,它只適用於應用於精靈的材質,即白色,是一個精靈/默認着色器。這聽起來可能不是一個大問題,但是每當我應用不同顏色的不同材質(例如藍色),並更改其設置以使其具有精靈/默認着色器時,精靈將變得不可見。

我是新的團結,如果有人可以幫助我,那將是非常讚賞

回答

0

我不知道你想要做什麼,但是這可能會幫助你。

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 
     } 

    } 

} 

}

我創建了一個材料,並在編輯器中它分配給遊戲對象。

+0

在控制檯中,它表示「此行爲的引用腳本已丟失!」。它沒有按預期工作。 –

+0

@NathanielOriecuia試試這個http://answers.unity3d.com/questions/347443/the-referenced-script-on-this-behaviour-is-missing-2.html – LongarMD

相關問題