- Home /
Function On GUI
Hello. I am using an OnGui Function that tells the gui to come up when the velocity of my game object is 0 however, sometimes when it hits objects (i can't have them as triggers they have to be collisions) the velocity goes to 0 for a fraction of a second causing the gui box to pop up the disappear.
When using OnGUI functions it doesn't allow you to do yield WaitForSeconds(2); before the GUI comes up. Here is an example of the code:
if (bigWheel.rigidbody.velocity.z < 1.0 && guiOn && powerThing.GetComponent(fire).choosing) {
}
so it's saying that if the rigidbody called bigWheel has a velocity of less than 1 and the componenet called fire is choosing (that just means when the big wheel has been launched from a power bar that i have) Within all of that is the code that constructs the GUI Layouts which isn't important to the problem.
So somewhere within that i need to say yield WaitForSeconds(2);
Does anyone know how?
function OnGUI (){
styleGreen.normal.textColor = Color.green;
styleRed.normal.textColor = Color.red;
if(bigWheel){
if (bigWheel.rigidbody.velocity.z < 1.0 && guiOn && powerThing.GetComponent(fire).choosing) {
var xPos = ( (Screen.width/2) - (menuWidth/2) );
var yPos = ( (Screen.height/2) - (menuHeight/2) );
GUILayout.BeginArea( Rect(xPos, yPos, menuWidth, menuHeight) );
GUILayout.BeginVertical("Box");
GUILayout.Label("You Travelled Over 190m Level Completed");
GUILayout.Space(10);
GUILayout.Label("Distance Travelled: " + dist.ToString("f0"));
GUILayout.Space(10);
GUILayout.Label("Your Score is:" + scorer);
}
}
}
Answer by Bunny83 · Mar 23, 2011 at 02:03 PM
WaitForSeconds won't help much. You need a delay that can be aborted or a condition that can be retriggered.
I think I would go for the retrigger approach.
edit
Have you even tried my solution? If you take a closer look, i've inverted your condition because if the GUI should not be displayed the timeout have to be reset. So my condition checks if (the speed is greater than 1.0) or (guiOn is not true) or (choosing is not true) then the time will reset.
The resulting behaviour is exactly what you want... only if the speed is lower than 1 and guiOn is true and choosing is true the timeout will not be reset anymore. And after 2 sec. the GUI will be displayed. If anyone of those conditions change it will be hidden again.
var guiTimeout : float;
function OnGUI (){ if(!bigWheel) return; styleGreen.normal.textColor = Color.green; styleRed.normal.textColor = Color.red; if (bigWheel.rigidbody.velocity.z >= 1.0 || !guiOn || !powerThing.GetComponent(fire).choosing) { guiTimeout = Time.time; }
if (Time.time - guiTimeout >= 2.0)
{
var xPos = ( (Screen.width/2) - (menuWidth/2) );
var yPos = ( (Screen.height/2) - (menuHeight/2) );
GUILayout.BeginArea( Rect(xPos, yPos, menuWidth, menuHeight) );
GUILayout.BeginVertical("Box");
GUILayout.Label("You Travelled Over 190m Level Completed");
GUILayout.Space(10);
GUILayout.Label("Distance Travelled: " + dist.ToString("f0"));
GUILayout.Space(10);
GUILayout.Label("Your Score is:" + scorer);
}
}
ps. You just checked the velocity in z direction. Maybe that's ok in your case but the velocity in general would be rigidbody.velocity.magnitude
.
Not directly related to your question, but if you can avoid it, try not to do the GetComponent every frame, let alone every OnGUI (which could be multiple times a frame). Rather, get it once (e.g. in Start()).
Well, you're right. I've just copied his condition. But in fact GetComponent is not that bad. If you have performance problems you can and should cache everything you need but as long as it runs smoothly it's ok. Just FYI: all shortcut properties like transform, rigidbody, audio, animation, .... they all use GetComponent internally everytime you use it. Do you cache all your transforms? ;)
Oh yes, I realize it was just a copy/paste, it was just meant for the OP. And actually if I need to manipulate any component every frame, transforms included, I typically cache them. It always 'feels' weird though, because the accessors look like regular variables, so Transform myTransform = transform seems...wrong :)
Answer by Frank Clark · Mar 23, 2011 at 05:23 PM
If you do the above that you suggested it ignores the conditions put before it...
So all it does is wait a certain amount of time and brings the gui. the desired effect is that the gui can only come up if the gameobject is launched and if the velocity is less than 1 then the gui comes up after waiting for the desired amount of time. here is the full code.
function OnGUI (){
styleGreen.normal.textColor = Color.green; styleRed.normal.textColor = Color.red; if(bigWheel){
if (bigWheel.rigidbody.velocity.z < 1.0 && guiOn && powerThing.GetComponent(fire).choosing) {
var xPos = ( (Screen.width/2) - (menuWidth/2) );
var yPos = ( (Screen.height/2) - (menuHeight/2) );
GUILayout.BeginArea( Rect(xPos, yPos, menuWidth, menuHeight) );
GUILayout.BeginVertical("Box");
GUILayout.Label("You Travelled Over 190m Level Completed");
GUILayout.Space(10);
GUILayout.Label("Distance Travelled: " + dist.ToString("f0"));
GUILayout.Space(10);
GUILayout.Label("Your Score is:" + scorer);
} }
Don't post comments or new parts of your question as answer. Edit your question if you want to add some information. And watch the code formatting
Your answer
![](https://koobas.hobune.stream/wayback/20220613114403im_/https://answers.unity.com/themes/thub/images/avi.jpg)