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 VLunarFangV · May 05, 2013 at 12:46 PM · javascriptsimpleongui error

Temporary GUI

I'm working on a small bit of code right now to have something show up on the screen but only for 3 seconds. I've gotten this far, but when I run the code it says "Script Error OnGUI () cannot be a coroutine." The debug code does not trigger either.

 function OnGUI () {
     if (Collect == true) {
         GUI.Label (Rect (10, 10, 100, 20), Gem + (" gems found"));
         Debug.Log ("On");
         yield WaitForSeconds (3);
         Collect = false;
         Debug.Log ("Off");
     }
 }

So, to anyone who knows what I'm doing wrong: Are you kind enough to share your knowledge with me?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ExTheSea · May 05, 2013 at 01:02 PM

You can't put yield into functions like OnGUI or Update but you could do something like this:

 function OnGUI () {
 if (Collect == true) {
 GUI.Label (Rect (10, 10, 100, 20), Gem + (" gems found"));
 Debug.Log ("On");
 }
 }
 
 function Only3Seconds(){
    Collect = true;
    yield WaitForSeconds (3);
    Collect = false;
    Debug.Log ("Off");
 }

If you then want to start it you use this line:

 yield StartCoroutine("Only3Seconds");
Comment
Add comment · Show 21 · 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 VLunarFangV · May 05, 2013 at 01:31 PM 0
Share

That's great and all, but I'm getting the message "OnGUI() cannot be a coroutine"

avatar image ExTheSea · May 05, 2013 at 01:36 PM 0
Share

Are you starting the Coroutine in the Update Function? If that's the case try just

 StartCoroutine("Only3Seconds");

without the yield.

avatar image VLunarFangV · May 05, 2013 at 01:46 PM 0
Share

I tried that and it's still being a pain from the very moment I start the test.

avatar image ExTheSea · May 05, 2013 at 02:15 PM 0
Share

What do you mean by it's a pain? Do you get any errors? Also when do you start the coroutine?

avatar image ExTheSea · May 05, 2013 at 04:05 PM 1
Share

You know what. Just try this: Delete your old script (or copy it somewhere before if you want) and make a new one and copy this inside:

 static var Gem = 0; static var Collect : boolean = false;
 function OnGUI () {
     if (Collect == true) {
        GUI.Label (Rect (10, 10, 100, 20), Gem + (" gems found"));
        Debug.Log ("On");
     }
 }
 function Wait3Seconds(){
     Start.Collect = true;
     yield WaitForSeconds (3);
     Start.Collect = false;
     Debug.Log ("Off");
 }
 function Update(){
     if(Input.Get$$anonymous$$eyDown("p"))
     StartCoroutine("Wait3Seconds");
 }

This is your code modified so that it works. I just created a Start.js and copied this in and it worked.

Show more comments
avatar image
0
Wiki

Answer by DryTear · May 05, 2013 at 04:25 PM

try this:

 var Collect : boolean;
 
 function OnGUI()
 {
     if(Collect == true)
     //Do your Things here
 }
 
 function Update()
 {
     if(Collect == true)
     {
         RemoveGUI();
     }
 }
 
 function RemoveGUI()
 {
     yield WaitForSeconds(3);
     Collect = false;
 }
Comment
Add comment · 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

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

15 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Simple Animation for Unity3d 1 Answer

How to switch off two lights? 2 Answers

Adding Names 1 Answer

How to add aceleration to a sphere by pushing a key? 4 Answers

Destroy object on collision and update script. 2 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