GUILayout.Button and GUI.DrawTexture in a Loop

Hello,

I have a question about Buttons and Textures drawn in a Button creation loop - why aren’t they painted?

I am basically crating buttons in a for loop:

for (i=0; i<=5; i++) {
    if (GUI.Button (new Rect (Screen.width/2 - 100*i, Screen.height/2 - 100*i, 64, 64), bText) {
        if (GUI.Button (new Rect (Screen.width/2, Screen.height/2, 64, 64), cText)) {
            Debug.Log ("Hello there.")
}
}
}

Same, instead of creating a subsequent GUI.Button, but a GUI.DrawTexture, they are not created. I can only see the first buttons created, but not the button or texture drawn, when pressing the button.

How do I tackle with this? It is honestly getting troublesome for my project to progress.

Thank you a lot for your attention!

I presume you can see the buttons that are created by the for (from 0 to 5), but you can’t see the button that is created when you press another button. That’s because this button is displayed in one frame right after the press button action was made.
If you want to make that button visibile, your should use a boolean to store your button action, like this:

    string bText;
	string cText;
	bool showButton;

	void OnGUI() {

		for (int i = 0; i <= 5; i++) {
			if (GUI.Button (new Rect (Screen.width/2 - 100*i, Screen.height/2 - 100*i, 64, 64), bText)) {
				showButton = true;
			}
		}

		if(showButton) {
			if (GUI.Button (new Rect (Screen.width/2, Screen.height/2, 64, 64), cText)) {
				Debug.Log ("Hello there.");
				showButton = false;
			}
		}
	}