I have a system in place where the player can talk to the NPCs, everything seems to work fine but I have to press the GUI button twice before it updates the screen! Help!
Code for the gui:
public void OnGUI(){
if(_guiEnabled==true){
GUI.Box(new Rect(10,Screen.height/2+115,Screen.width-15,23),response[1]);
for(int i=0;i<speech.Length;i++){
if(speech[i,1]!=null){
if(GUI.Button(new Rect(10,Screen.height/2+(140+(25*i)),Screen.width-15,23),speech[i,1])){
//Log response to master table
//Setresponse(response[2],Convert.ToInt32(response[3]),response[4],Convert.ToInt32(response[5]),Convert.ToInt32(speech[i,0]),Convert.ToInt32(response[0]));
Debug.Log("Clicked button!");
_currentorder=Convert.ToInt32(speech[i,2]);
_recentresponse=Convert.ToInt32(response[0]);
//Get new speech and responses
getSpeech(_recentresponse);
getResponses(_currentorder);
}
}
}
}
}
Edit:
Just wanted to try and clarify: The GUI.Box updates perfectly, but you have to push the button twice before it will load the correct values for the buttons.
No one knows what is going on?
I guess is because GUI.Button is inside the for loop, try switching things like this code below plz and tell me if it worked…
public void OnGUI(){
if(_guiEnabled==true){
GUI.Box(new Rect(10,Screen.height/2+115,Screen.width-15,23),response[1]);
if(GUI.Button(new Rect(10,Screen.height/2+(140+(25*i)),Screen.width-15,23),speech[i,1])){
for(int i=0;i<speech.Length;i++){
if(speech[i,1]!=null){
//Log response to master table
//Setresponse(response[2],Convert.ToInt32(response[3]),response[4],Convert.ToInt32(response[5]),Convert.ToInt32(speech[i,0]),Convert.ToInt32(response[0]));
Debug.Log("Clicked button!");
_currentorder=Convert.ToInt32(speech[i,2]);
_recentresponse=Convert.ToInt32(response[0]);
//Get new speech and responses
getSpeech(_recentresponse);
getResponses(_currentorder);
}
}
}
}
}
Your code is basically making a lot of GUI buttons in the same position.
You will need to rework it so that it is not in a loop, I’m not sure if what @ devprincess did will work for you but that is the problem.
Thank you two for your response! devprincess, that won’t work as the button has to be in the loop since the loop is what makes it generate multiple buttons, also the loop is used to determine the height.
Actually each button is positioned exactly below each other due to the way that I have the “top” coded. But I thank you both so much for your replies because you helped me realize what is wrong!
Turns out I have to move away from arrays since they don’t expand dynamically and thus do not allow me to get the proper array length, I’m going to have to use an ArrayList or a List.