- Home /
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);
}
}
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).
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?
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.
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
Follow this Question
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