- Home /
Changing a variable to the next one in order automatically..
Currently I'm using GUI.Label and I am using a variable in the string section to tell it what string to display. There is a lot of different text to go to and from and I am trying to figure out how to tell that variable to change to a new string.
The basic setup:
string slide1 = "Slide 1 test";
string slide2 = "Slide 2 test";
void OnGUI()
{
GUI.Label(new Rect(50, Screen.height-130, Screen.width-100, 100), slide1, "TextField");
}
void Update(){
if(Input.GetMouseButtonDown(0))
{
??????????????????????????
}
}
I could manually tell it after mouse down to slide1 = slide2; or something but then what do I do for slide3 and 4 and so on and so on? I could just tell it forever and ever to keep redoing the thing manually (if on slide1 go to slide2 if on slide2 go to slide3 etc.) but that seems like it'd be really inefficient and I'm missing some fundamental programming skill I should have to resolve this.
Answer by perchik · Feb 13, 2014 at 04:07 PM
I think you need to create a list. If you put all your strings in a list and then keep an integer that points to which place you are in the list. Something like this: (fair warning, this code probably doesn't work, but should give you the right idea)
List<string> myStrings = new List<string>();
myStrings.Add("Message1");
myStrings.Add("Message2");
int indx= 0;
void Update(){
if(Input.GetMouseButtonUp(0)){
indx = indx +1 % myStrings.Length();
//Add one to the index, but make it wrap around
}
}
void OnGui(){
GUI.Label(new Rect(50, Screen.height-130, Screen.width-100, 100), myStrings[indx],"TextField");
}
Eww. If you're not adding or removing elements to it, do not use a list. They have more verbose syntax and are slightly slower than their fixed-length alternative, arrays. Use arrays wherever possible - they are incredibly fast. If you don't need to add or remove elements at runtime, then don't use lists.