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 JakeLeB · Mar 27, 2015 at 09:44 AM · guifloatforeachwhilewhile-loop

GUI inside a While Loop

I have two Floats, yOffset and playerCount. I'm using PUN and trying to give a HUD for each player joined. But it seems to be putting all of the HUDs at exactly the same height... I thought if I had a while loop that checks if the current rooms player count is higher than the float I created and added to the float after running the DrawTexture it would put them at different heights, but I can't seem to figure this out and would really appreciate some help.

 public float yOffset = 0;
 public float playerCount;

 while (PhotonNetwork.room.playerCount > playerCount) {
             foreach (PhotonPlayer player in PhotonNetwork.playerList) {
                 GUI.DrawTexture (new Rect (355, yOffset, 100, 50), HealthBar);
             }
             if(playerCount != PhotonNetwork.room.playerCount) {
                 yOffset += 45;
                 playerCount += 1;
             }
         }


...................

 for(int i = 0; i < PhotonNetwork.playerList.Length; i++){
             foreach (PhotonPlayer player in PhotonNetwork.playerList) {
                 GUI.DrawTexture(new Rect (0, i*(60) + 10, 150, 50), HUDBox);
                 GUI.DrawTexture(new Rect (0, i*(60) + 40, 150, 10), HealthBar);
                 GUI.Label(new Rect (0, i*(65) + 10, 150, 50), player.name);
             }
         }
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
1

Answer by siaran · Mar 27, 2015 at 04:08 PM

Well, I see your problem - too many loops and if statements and I don't even know what it does. I can't quite follow what your code actually does, never mind what goes wrong...how about you do this instead?

 for(int i = 0; i < PhotonNetwork.playerList.Length; i++){
  GUI.DrawTexture(new Rect 355, i*(50+yOffset), 100, 50), HealthBar);
 }

You could use PhotonNetwork.room.playerCount as well instead of playerlist.Length, but I figure this way it makes more sense if you later add something like PhotonNetwork.playerList[i].someProperty in the for loop (I don't remeber what properties player objects have in Photon and if you'd ever want to do that, but still).

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 JakeLeB · Mar 28, 2015 at 01:25 AM 0
Share

Well the reason I was using a while loop is because using a foreach loop continously adds to the yOffset float and results in the hud scrolling to the bottom, would I replace all of the code above with this?

avatar image Kiwasi · Mar 28, 2015 at 03:56 AM 0
Share

For loop is definitely the way to do this in Legacy.

Any reason you are using legacy for this? The new UI is much better.

avatar image JakeLeB · Mar 28, 2015 at 05:14 AM 0
Share

I gave it a shot but in all fairness, I much prefer scripting the UI as opposed to having to have separate canvases that are linked to scripts to do what can be done in less code.

Your code works amazingly, just one small issue I'm having is the reason for the foreach loop was so I could print out their name and health, this works but each new player draws a new set of huds on top of the existing. I've updated the main post with the code.

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

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

Related Questions

How To Make A Floating Health Bar 1 Answer

How to get something to be inactive while there is a certain value 2 Answers

Number 0 not showing up in GUI.Button 2 Answers

Display a loading while processing a while loop 1 Answer

While loop freezes unity 3 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