- Home /
turn gui label red on key down (js)
Hello, i am now stuck. how would i turn the gui label "boost" red when the shift key is held down and the toggle back to black when it is released?
thanks for reading
var timerInSecond = 0;
private var levelTimer = 0.0;
private var updateTimer = false;
var currentTime;
var style : GUIStyle;
function Start()
{
updateTimer = true;
levelTimer = 0.0;
}
function Update()
{
if (updateTimer)
levelTimer += Time.deltaTime*1;
/// float to int
timerInSecond = Mathf.Round (levelTimer);
currentTime = levelTimer;
}
function LevelEnded()
{
updateTimer = false;
///Save Time
PlayerPrefs.SetInt("Time In Second", timerInSecond );
}
function OnTriggerEnter(box : Collider){
if(box.gameObject.tag == "box"){
Debug.Log("timerfinished");
updateTimer = false;
}
}
function OnGUI () {
GUI.Label(new Rect(20,20,400,90), "Time = " + levelTimer,style );
GUI.Label(new Rect(20,150,400,90), "Boost",style );
}
Comment
Answer by HenryStrattonFW · Nov 06, 2015 at 04:43 PM
In unity, GUI draws use a common colour for their tint. GUI.color.
to tint certain elements you have to alter this value, and then set it back to ensure further GUI calls dont all get tinted as well. try this as your GUI method.
function OnGUI ()
{
Color lPrevColour = GUI.color;
GUI.Label(new Rect(20,20,400,90), "Time = " + levelTimer,style );
if(Intput.IsKeyDown(KeyCode.LeftShift))
{
GUI.color = Color.red;
}
GUI.Label(new Rect(20,150,400,90), "Boost",style );
GUI.color = lPrevColour;
}
Your answer
Follow this Question
Related Questions
GUI controlled by keyboard 4 Answers
How to change the arrpearance of the mobile keyboard 1 Answer
Is there a way to combine the Update and OnGUI functions? 1 Answer
Press Keyboard Instead Of Mouse 2 Answers
TextField not getting focus on iOS 1 Answer