0

i have a problem with printing GUI elements in for loop pf OnGUI() method. In code listed below i create GUI.Label of user clicked on GUI.Box:

public List<string> messages = new List<string>();     
int dialogueMsg = 0;
     void OnGUI()
         {
             Event currentEvent = Event.current;
             if (enabled)
             {
                 GUI.BeginGroup(new Rect(Screen.width / 8, Screen.height / 4 + Screen.height / 2, Screen.width - Screen.width / 4, Screen.height / 4));
                 GUI.Box(new Rect(0, 0, Screen.width - Screen.width / 4, Screen.height / 4), "");
                 if (currentEvent.button == 0 && currentEvent.type == EventType.mouseDown)
                 {
                     for (int i = 0; i < messages.Count; i ++)
                     {
                         if (dialogueMsg < messages.Count)
                         {
                             print(dialogueMsg);
                             print(messages[dialogueMsg]);
                             GUI.Label(new Rect(25, 25, Screen.width - Screen.width / 4, Screen.height / 4), messages[dialogueMsg]);
                             ++dialogueMsg;
                             break;
                         }
                         else
                         {
                             enabled = !enabled;
                             break;
                         }
                     }
                 }
                 GUI.EndGroup();
             }
         } 

And when user clicked - nothing happend, except printing correct values dialogueMsg and messages[dialogueMsg]. What am i doing wrong? Did anybody is familiar with this problem?

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Supo
  • 1,135
  • 3
  • 8
  • 10
  • When your if statement, inside the for loop, are you wanting it to break out of the for loop the first time it is run? in both the if and else statement you have the break keyword which will break you out of the for loop regardless of if the count (i < messages.Count()) is exceeded. – CalebB Feb 02 '15 at 20:36
  • Oh, i get it. I had just to set new String variable, that stores messages[dialogueMsg] value and stand GUI.Label outside for-loop. Thx for the answer! – Supo Feb 02 '15 at 23:37
  • @CalebB Might want to post it as an answer so the OP can accept it – MX D Feb 04 '15 at 13:16

1 Answers1

0

In both the if and else statements you have the break keyword, thus the for loop will only run once then break out of the loop, regardless of if i < messages.Count() or not. Just take out the break keyword from the if or else, or both, code block(s) where you want the loop to continue then you should be all set for that part.

CalebB
  • 597
  • 3
  • 17
  • I've already do it, and nothing changes. And there is very odd problem: I separate OnGUI method, make new MyMethod() that holds GUI.Label call in it... and it works! I post my code, when i will be at home – Supo Feb 04 '15 at 14:50
  • What is your desired action from this method? I see that it should create and populate GUI Labels with your messages but is there something else that's suppose to take place in addition to this? Can you add more surrounding code possibly? – CalebB Feb 05 '15 at 21:14