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 potatoking · Dec 30, 2013 at 07:45 PM · c#debugongui

Part of a function called from OnGUI doesn't work C#

The second part of my function 'DisplayUnits' that i call from OnGUI sometimes doesn't run (/get "played"). This is my code:

 void OnGUI () {
         GUI.color = bla;
         if(myGameData.isInGame){
             DisplayUnits (null, scaledRect.sSquare (10, 10, 40), true);
             DisplayUnits (myGameData.charData.weapons[myGameData.charData.CurrentWeapons[0]],
                           scaledRect.sSquare (15, 145, 40), false);
             DisplayUnits (myGameData.charData.weapons[myGameData.charData.CurrentWeapons[1]],
                           scaledRect.sSquare (245, 145, 40), false);
         }
     }
 
     private void DisplayUnits (Weapon w, Rect pos, bool disLife){
         Texture icon;
         float amount;
         if(disLife){
             amount = myGameData.lives;
             icon = lifeTexture;
         }
         else{
             if(w.level == 0)
                 return;
             amount = w.currentAmmo;
             icon = w.icon;
         }
         if(amount < 1)
             return;
 
         Rect iconRect = new Rect(pos.center.x - pos.width / 3.2f, pos.center.y - pos.height / 3.2f, pos.width / 1.6f, pos.height / 1.6f);
         GUI.DrawTexture (pos, middle);
         GUI.DrawTexture (iconRect, icon);
         Matrix4x4 original = GUI.matrix;
         for(float layer = 0; layer < amount / 3; layer++){
             for(int cnt = 0; cnt < 3; cnt++){
                 if(layer * 3 + cnt + 1 > amount)
                     return;
                 float angle = myGameData.gametime * 20 + cnt * 120 + layer * 30 + myGameData.gametime * Mathf.Pow (-layer, layer) * 10;
                 float scale = 1.1f + (layer * .2f);
                 GUIUtility.RotateAroundPivot (angle, pos.center);
                 GUIUtility.ScaleAroundPivot(new Vector2(scale, scale), pos.center);
                 GUI.DrawTexture (pos, border);
                 GUI.matrix = original;
             }
         }
 
         //Cooldown
         if(!disLife){
             if(debug){
                 Debug.Log (myGameData.gametime);
                 Debug.Log (w.coolDown[w.level]);
                 Debug.Log (w.lastUsed);
                 debug = false;
             }
             if(myGameData.gametime - w.lastUsed > w.coolDown[w.level] || w.lastUsed == 0){
 
             }
             else{
                 GUIStyle gStyle = new GUIStyle();
                 gStyle.fontSize = 60 * Screen.width / 900;
                 GUI.Label (iconRect, ((int)(w.coolDown[w.level] - (myGameData.gametime - w.lastUsed))).ToString (), gStyle);
             }
         }
     }

The part that doesn't seem to get played is after the comment //Cooldown. To find what the problem was, I exposed a public bool 'debug' in the editor. When I turned this bool on, the debug statement doesn't do anything, leading me to the conclusion that the second part doesn't get played, however the gui elements in the first part of the function: DisplayUnits, get displayed and keep spinning, so that part works.

I don't seem to be able to find what I did wrong, so I would appreciate any help.

*The return statements in the begin of the function aren't the cause of the problem since the gui textures in the first part are still showing.

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

1 Reply

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

Answer by Spinnernicholas · Dec 30, 2013 at 07:46 PM

It might also be:

 if(layer * 3 + cnt + 1 > amount)
   return;

I think you want continue:

 if(layer * 3 + cnt + 1 > amount)
   continue;


[OLD] This code:

 if(amount < 1)
          return;

causes the method to return sometimes. Which prevents anything else in the method from running after it.

Comment
Add comment · Show 6 · 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 potatoking · Dec 30, 2013 at 07:59 PM 0
Share

if that was the cause of the problem, i wouldn't be able to see the gui elements that i used after the return statement, so that can't be the problem :/

avatar image Spinnernicholas · Dec 30, 2013 at 08:34 PM 0
Share

Can you pinpoint where it cuts off and what do you mean by doesn't get "played". I thought that meant the same as doesn't run, which would prevent some of the gui from appearing. Are you saying that the Gui is displaying 100% correctly, but isn't responding to interaction?

avatar image potatoking · Dec 30, 2013 at 08:37 PM 0
Share

The GUI before the comment: //cooldown runs correctly, but after that, it doesn't seem to work, because the gui and debug statements after that don't get displayed, and the public bool 'debug' doesn't get changed to false

avatar image Spinnernicholas · Dec 30, 2013 at 10:26 PM 0
Share

I updated answer.

avatar image potatoking · Dec 31, 2013 at 09:22 AM 0
Share

Yeah that was it, Thank you! :)

Show more comments

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

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

Related Questions

GUILayout not working for me C# 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Can Unity track the changes I made to my DLL? 0 Answers

What's wrong with my code? C# 1 Answer


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