2016-08-02 10 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

関連する問題