Hello. I’m trying to get my GUI to off after 3 seconds but it’s not working. My GUI stays on all the time after hitting the cube. I tried to use the yield statement in the OnGUI function but it ends up turning the GUI off entirely. How do I off it after a certain amount of time? Thank you.
Here’s my code:
#pragma strict
var showGUI = false;
var otherScript : PlayerRelativeControl;
var increase = 5.0;
var temporarySpeed = 0.0;
var speedBoostDuration = 4;
var nextActivationTime = 0.0;
private var timer = 0.0;
private var horizontalSpeed = Input.GetAxis("Horizontal");
private var verticalSpeed = Input.GetAxis("Vertical");
function Update()
{
rigidbody.mass = 0.1;
var otherScript = GetComponent(PlayerRelativeControl);
}
function OnControllerColliderHit (collisionObject: ControllerColliderHit)
{
if(collisionObject.gameObject.name == "Cube" && Time.time > nextActivationTime)
{
nextActivationTime = Time.time + speedBoostDuration;
Destroy(collisionObject.gameObject);
temporarySpeed = otherScript.forwardSpeed +increase;
showGUI = true;
yield WaitForSeconds(3);
temporarySpeed = otherScript.forwardSpeed;
rigidbody.AddTorque(Vector3(0,horizontalSpeed,verticalSpeed) * 10);
rigidbody.AddForce (Vector3(temporarySpeed,0,0));
showGUI = false;
}
}
function OnGUI ()
{
if(showGUI)
{
GUI.enabled = showGUI;
GUI.Label (Rect (Screen.width/2, Screen.height/3, 600, 40), "Speed Increase!");
}
}
Thank you. Cheers.
Why are you setting rigidbody.mass to .1 and doing "var otherScript = GetComponent(PlayerRelativeControl);" every frame in Update? Especially since you're not even using that variable for anything.
– Eric5h5In what way? You're declaring a variable that you're not using, every frame.
– Eric5h5