2016-06-11 75 views
0

嗨我腳本有一些錯誤。也許你可以幫忙。噢......Unity中的C#錯誤

我想讓我的文字變得豐富多彩,每一秒都有新的不同顏色。但是當我編寫腳本時就會出現這樣的錯誤。你可以幫幫我嗎 ? 它給了Unity中的錯誤。

錯誤CS0619:UnityEngine.Component.renderer' is obsolete:物業渲染器已被棄用。改用GetComponent()。 (UnityUpgradable) '

錯誤CS1061UnityEngine.Component' does not contain a definition for材料類型',沒有擴展方法material' of type UnityEngine.Component」可以找到(是否缺少using指令或程序集引用?)

而腳本

using UnityEngine; 
using System.Collections; 

public class Colors : MonoBehaviour 

{ 
public float timer = 0.0f; 

void Start() 
{ 

} 


void Update() 
{ 
    timer += Time.deltaTime; 
    if (timer >= 2.0f)//change the float value here to change how long it takes to switch. 
    { 
     // pick a random color 
     Color newColor = new Color(Random.value, Random.value, Random.value, 1.0f); 
     // apply it on current object's material 
     renderer.material.color = newColor; 
     timer = 0; 
    } 


} 
} 
+1

錯誤CS0619明確規定你必須做的:用GetComponent (),而不是渲染器,因爲它已被棄用 – TheDjentleman

回答

0

您再也不能從Unity 5起直接訪問renderer.material.color了。您必須首先使用GetComponent<Renderer>();來獲取GameObject的組件,然後才能從Renderer訪問該材質。

public float timer = 0.0f; 
Renderer rd; 

void Start() 
{ 
    rd = gameObject.GetComponent<Renderer>(); 
} 


void Update() 
{ 
    timer += Time.deltaTime; 
    if (timer >= 2.0f)//change the float value here to change how long it takes to switch. 
    { 
     // pick a random color 
     Color newColor = new Color(Random.value, Random.value, Random.value, 1.0f); 
     // apply it on current object's material 
     rd.material.color = newColor; 

     timer = 0; 
    } 
} 
+0

Thank.I會嘗試。 – OneARMINAS

+0

「文本」遊戲對象沒有附加「渲染器」。現在做什麼 ? – OneARMINAS

+0

@OneARMINAS發生什麼錯誤? – Programmer