- Home /
Turning GUI off not working for iOS
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.
I thought that would help to connect the 2 scripts together.
In what way? You're declaring a variable that you're not using, every frame.
Okay I have taken out the function. But the GUI ("Speed Increase") still stays there after 3 secs.
Answer by aldonaletto · Sep 07, 2011 at 01:38 PM
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"); } ...
Ooops! I forgot to declare the types of horizontalSpeed and verticalSpeed. Answer fixed now.
Hello! it works fine now but the GUI (""Speed Increase!" ) did not turn off after the speed went back to the default speed. What should I do? Thank you so much for helping!! :)
Hello! I have managed to solve the problem by setting another function to enable the showGUI to true, yield for 3 seconds and offed it after that. Thank you for your help! :)) Cheers!!
The mistery persists: Speed Increase turned off in my PC after 3 seconds, as expected. Could it be some iOS version bug? God knows! Anyway, what really matters is that you solved the problem.
goodness gracious... i'm not sure. i'm currently using the trial version of Unity Pro. :) thanks a lot!! cheers! :)
Your answer

Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Setting Scroll View Width GUILayout 1 Answer
Trigger a GUI when tapping and object in IOS 1 Answer
Find the Vector to divert from colliding? 1 Answer
Is it possible to link character skill lists to a GUI, and if so, how? 3 Answers