system
1
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.
I tested this script in my PC, and it produced some runtime errors; when I fixed them, it worked fine. The errors were:
1- You should remove var from the line where you get otherScript via GetComponent - this keyword was creating a temporary variable, so the public otherScript never was assigned. By the way, you should place this code in Start, since it only needs to be executed once;
2- You must move both Input.GetAxis to the Update function (or any other). Input.GetAxis outside any function produces a runtime error.
All the changes were situated at the same region, so I show below only the modified part of the code:
...
private var timer = 0.0;
private var horizontalSpeed: float;
private var verticalSpeed: float;
function Start(){
rigidbody.mass = 0.1;
otherScript = GetComponent(PlayerRelativeControl);
}
function Update(){
horizontalSpeed = Input.GetAxis("Horizontal");
verticalSpeed = Input.GetAxis("Vertical");
}
...