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 Frank Clark · Mar 23, 2011 at 12:50 PM · yieldwaitforsecondsongui

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 &lt; 1.0 &amp;&amp; guiOn &amp;&amp; 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);
     }
 }

}

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

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 &gt;= 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.

Comment
Add comment · Show 3 · 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 Molix · Mar 23, 2011 at 02:19 PM 0
Share

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()).

avatar image Bunny83 · Mar 23, 2011 at 02:45 PM 0
Share

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? ;)

avatar image Molix · Mar 23, 2011 at 06:19 PM 0
Share

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 :)

avatar image
-1

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 &lt; 1.0 &amp;&amp; guiOn &amp;&amp; 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);

} }

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 Bunny83 · Mar 23, 2011 at 07:00 PM 0
Share

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

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

No one has followed this question yet.

Related Questions

yield WaitForSeconds in OnGUI 4 Answers

Why Is yield return new WaitForSeconds() not working 2 Answers

Use WaitForSecounds in OnGUI() 1 Answer

Definition of Script.function() depends on Script.function()... 3 Answers

Understanding yield inside a for or a while loop 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