Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by roncel106 · Aug 04, 2012 at 07:20 PM · androidbuttonyielddelayongui

How to add delay before showing GUI Button?

I'm trying to make a restart button but I always get this error that OnGUI can't be a coroutine. so how do i add a delay without using yield?

Here's my code:

function OnGUI(){

 if(gameOver == true)
 {    
     yield WaitForSeconds (gameOverDelay);
     Time.timeScale = 0.0;
     if (GUI.Button (Rect(20,20,100,20), "Restart")) {
         Application.LoadLevel(Application.loadedLevel);
         gameOver = false;
         Time.timeScale = 1.0;
     }
 }

}

Could you please show me the code appropriate for this? and could you explain the error for me (sorry i'm kinda new at this). Thank you :D

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ScroodgeM · Aug 04, 2012 at 07:37 PM

var ShowButtonTime : float;

function ShowButtonWithDelay() { ShowButtonTime = Time.time + gameOverDelay; }

function OnGUI(){

 if(gameOver == true && Time.time >= ShowButtonTime)
 {    
     Time.timeScale = 0.0;
     if (GUI.Button (Rect(20,20,100,20), "Restart")) {
         Application.LoadLevel(Application.loadedLevel);
         gameOver = false;
         Time.timeScale = 1.0;
     }
 }

}

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image roncel106 · Aug 04, 2012 at 08:38 PM 0
Share

soo... when did you use the function ShowButtonWithDelay()?

avatar image ScroodgeM · Aug 04, 2012 at 09:35 PM 0
Share

right at moment of gameover

avatar image
0

Answer by aldonaletto · Aug 04, 2012 at 07:40 PM

Start a timer when gameOver becomes true, and when this timer ends enable the button with the variable restartEnabled:

private var restartEnabled = false; private var tEnd: float = 0;

function Update(){ // if gameOver became true... if (gameOver){ if (tEnd == 0){ // set the timer tEnd = gameOverDelay; } else { // and decrement it in the next Updates tEnd -= Time.deltaTime; if (tEnd <= 0){ // if timer ended enable button restartEnabled = true; } } } }

function OnGUI(){ if (restartEnabled){ if (GUI.Button (Rect(20,20,100,20), "Restart")) { Application.LoadLevel(Application.loadedLevel); } } }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image roncel106 · Aug 04, 2012 at 08:34 PM 0
Share

Thanks a bunch! Never thought of that. :D

avatar image
0

Answer by drawcode · Aug 04, 2012 at 08:44 PM

You can still use a coroutine and just put all your game over code behind it from StartCoroutine().

 function OnGUI(){
     if(gameOver == true)
     { 
         StartCoroutine(gameOverCoroutine());
     }
 }

 IEnumerator gameOverCoroutine() {
     yield WaitForSeconds (gameOverDelay);
     Time.timeScale = 0.0;
     if (GUI.Button (Rect(20,20,100,20), &quot;Restart&quot;)) {
         Application.LoadLevel(Application.loadedLevel);
         gameOver = false;
         Time.timeScale = 1.0;
     }
 }

OR

you can also just call Invoke("methodName", delay); to call something after a certain amount of time.

 var gameOver = false;
 var gameOverDelay = 5f;
 
 function OnGUI(){
     if(gameOver == true)
     { 
         Invoke("gameOverDelayed", gameOverDelay);
     }
 }
 
 function gameOverDelayed() {
     Time.timeScale = 0.0;
     if (GUI.Button (Rect(20,20,100,20), "Restart")) {
         Application.LoadLevel(Application.loadedLevel);
         gameOver = false;
         Time.timeScale = 1.0;
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image roncel106 · Aug 05, 2012 at 12:50 AM 0
Share

I tried both, and i keep on getting errors.

Invoke: GUI Buttons are for OnGUI only

Coroutine: It keeps on saying that I need to put ; or } at the end.

avatar image drawcode · Aug 05, 2012 at 03:38 AM 0
Share

True sorry I never use OnGUI anymore as I use a UI toolkit and forgot about that restriction. Aldo's response will work with OnGUI elements perfectly.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

i need to delay an action but my code isnt working 1 Answer

iPhone Touch Screen? 2 Answers

Unity IAP Codeless not Working on Android Device after Application.Quit() 0 Answers

How do I edit the first person controller to work with android? 0 Answers

AndoidTouchControllJavaRaycast 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges