2016-06-11 125 views
1

我的腳本適用於3D遊戲對象,但不適用於Text組件。我如何使它在文本上工作?我會很感激你的幫助。如何更改UI文本顏色

using UnityEngine; 
using System.Collections; 

public class Colors : MonoBehaviour 

{ 
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

無論誰提出了問題 - 請編輯它,所以它實際上包含問題(「請幫助」是*不是*問題)。 –

+0

您不能以這種方式使用Update()。只需使用** Invoke **製作一個計時器即可。這很容易。 – Fattie

+0

你能告訴我嗎?我是編程的新手。 – OneARMINAS

回答

0

使用者介面文字,你用Text.colorRenderer.material.colorRenderer用於連接到它們的Mesh Renderer的3D對象。請確保將其附加到遊戲對象上,並附上Text組件,否則您將收到錯誤消息。

public float timer = 0.0f; 
Text txt; 

void Start() 
{ 
    txt = gameObject.GetComponent<Text>(); 
} 


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 
     txt.color = newColor; 

     timer = 0; 
    } 
} 

Here是一個適合您的Unity教程。在提出進一步問題之前,請至少做一個項目。