- Home /
How do I delay the appearance of these buttons?
function OnGUI()
{
GUI.Box(Rect(((Screen.width)/2)-45,(((Screen.height)/2)-25)-60,90,50), "Score: " + playerScript.score, fontVar); //Shows user's score (I want that to show immediately)
//I want the pause here. I've tried yield WaitForSeconds(1); but it tells me that OnGUI() cannot be a coroutine.
if(GUI.Button(Rect(((Screen.width)/2)-45,(((Screen.height)/2)-25),90,50), "Play Again"))
{
Application.LoadLevel("sceneLevel1");
}
if(GUI.Button(Rect(((Screen.width)/2)-75,(((Screen.height)/2)-25)+60,150,50), "Return to Main Menu"))
{
Application.LoadLevel("sceneScreenMainMenu");
}
if(GUI.Button(Rect(((Screen.width)/2)-45,(((Screen.height)/2)-25)+120,90,50), "Exit Game"))
{
Application.Quit();
}
}
Answer by AnomalousX12 · Feb 10, 2014 at 05:22 PM
Updated answer. This has actual time functionality. Once again, my C# Visual Studio friend helped me. Why were all of the answers here so much more complicated?
function OnGUI()
{
//Initial code
if(Time.time < 1)
{
}
else
{
//Delayed code
}
}
this is functional, BUT this is kinda difficult to track the time, since a second could have 6-7 OnGUI updates even more if lots of elements in scene, if you are going to use this method change it for
if(counter < 60)
counter++;
else
{
///your code...
}
this avoid to make unnecesary additions and stops an overflow of counter variable(which it would take about 9 years to happen at pace of 7 per second...)
Awesome! I implemented that. Is there no delta time use that I could fix the issue you have with this method?
Is this "newer verion even works? From what i know Time.time returns time that passed since level was started. So it would be almost every time greater than 1.
It works perfectly from what I can see so... I dunno.
Answer by DajBuzi · Feb 10, 2014 at 04:13 PM
I would do it like this:
//first i would set time offset and some kind of "checker" to know if time was set
public float dispOffset = 1.0f;
private bool tikeWasSet;
// ...
// next you need action to set when to display theM
if(someKindOfActionWasDone){
timeToDisplay = Time.time + dispOffset;
timeWasSet = true;
}
void OnGUI(){
if(timeWasSet && Time.time >= timeToDisplay){
//your delayed code here
}
}
Huh... That just seems so much more complex than what I thought would be needed :[ Is there really no simpler way? I don't even know what a lot of that stuff kinda meant. Like Time.time.
this is the right answer, a simpler way could be a yield waitForSeconds, but it will stop OnGUI, still, you have to read a little documentation like Time.time, to know what they are and what you can do with them
I believe my friend came up with a simpler answer. I submitted it as an answer for other people who have this question.
Answer by JeffreyD · Feb 10, 2014 at 05:22 PM
How about... void OnGUI(){ Invoke ("DoMyButtonsDelayed", 1.50f); // delay time is second argument }
void DoMyButtonsDelayed() { //your delayed code here }
Your answer
Follow this Question
Related Questions
How can I fix this Coroutine Camera? 1 Answer
Enemy AI; Run away from player (x and y) 1 Answer
How do i need to change this script for mobile? 1 Answer
game won't work in build 0 Answers
Wiggle Or shake button possible? 2 Answers