GUI Game Help! There must be an easier way to do this!!

Hi Everyone,

I am making four player game where players answer questions through the GUI and it changes what happens in a 3D world. I am just starting this project and before I dig in too deep I want to see if anyone else has any better ideas on how to execute this.

So far I have been scripting everything in the function OnGui. However, I can already tell that to do this for four players I am going to need a ton of different boolean variables and the size of my code is going to get ridiculous.

Basically what I want to have happen is this. Player One puts in there name and presses a button to go on. That creates a new GUI.TextArea where the player can enter a new answer (string or value). This continues on and on. After questions where a string is required there is a fail safe that allows the player to go back and fix their answer (in case of misspelling).

Below is my code. As you can see the player can enter their name and then decide whether to go forward or back. I have not even started to ask my questions yet and I’m worried that with many questions and four players… that this is not only going to take forever but my code is going to be ridiculously long!!!

#pragma strict
 var ButtonOne : boolean = false;
 var GUIEnabledOne : boolean = false;
 var PlayerOne : String;
 var VerifyButtonOne : boolean = false;


  

function OnGUI (){ 

	if (GUIEnabledOne == false){
    	PlayerOne = GUI.TextArea (Rect(10,20,200,50), PlayerOne, 200);
    	GUI.Label (Rect (30, 0, 200, 50), "Player One Enter Your Name");
    }
    
    if (ButtonOne == false) {
    		if (GUI.Button (Rect (210,20,100,50), "Next")) {
    			GUIEnabledOne = true;
   				print(PlayerOne);
   				ButtonOne = true;
   				VerifyButtonOne = true;
   			}
   	}
   			
   	if (VerifyButtonOne == true){
   		if (GUI.Button (Rect (210,20,100,50), "Next")){
		}
		GUI.Label (Rect (110, 20, 100, 50), "Welcome " + PlayerOne);
		if (GUI.Button (Rect (0,20,100,50), "Back")){
   		}
   	}		
   		 
}

Maybe have a variable for current player.
var currentPlayer: string = “Player One”

Also, you can use functions:
if(ButtonOne == false) showButton();

You could have the script attached to 4 objects, and have a public variable that you can change in each instance of the script that changes the location of the gui stuff.

so that way you really only have to program it for 1 person.

Then you could have a random int that would be different for each person (as it is random, it being the same script wouldn’t affect it) with a certain domain and for each number you have a question corresponding to it.

You could also have a text file with the questions and answers and you derive the questions in the script from that. I have done similar things with text files and info and it’s really easy and cuts down the code.

I’ve actually tried the functions and I ran into some bugs. When I declared the function outside of the OnGUI and then tried to trigger it in the OnGUI nothing would show up. Do you think that I was doing something wrong?

I don’t think that the 4 object idea would work because each person will have different questions depending on when their turn comes up and turns rotate throughout the game.

However, I am really interested in the text file! Do you have a link to some documentation on that or an example on how you got it to work? I think that would work really well for this game.

Yes. Functions are separate things so you have to use arguments and returns for communication, or trigger public variables. There’s no reason not to use them. They break up your code and make it more readable, and can be more re-usable. A lot of times you can just have them return true or false and use them in an if statement.

Thanks fire7side. I will give them a shot. I rarely code or use Unity and so I am still fairly new to both. That’s probably why I was struggling using the functions earlier.