I am trying to iterate through the alphabet with a button. simple? not for me :).
I serialized the field so I could attach the button to it in it’s game object. If I type Debug.log, or Print, each letter shows up individually which means its working i think, I just can’t figure out how to show each letter after the iteration on the screen ( i don’t want to use the console, it is for a mobile app)?
for ( int i = 0; i < alphabet.Length; i ++){
// alphaText.text = alphabet*;* //alphaText.GetComponent().text = alphabet*;* } } both commented lines of code do the same thing. Starts out as A, then when button is clicked it goes straight to Z. Why?
you appear to be falling into the “usual” pitfall of thinking things move forward within the code… unless you are dealing with coroutines the entire function executes in a single frame. So the entire for loop executes and then the text is rendered to the screen for the frame.
If you want something to pause you need coroutines, to design the function to work over many Update calls (with timechecks or some sort of if/case/logical control); or given that you appear to want to have the app/game move forwards based on user input, simply drive the events from the user interactions.
i.e.
string[] alphabet = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
int pointer = 0;
void Start()
{
alphaText.text = alphabet[pointer]; // setup the text to start with
}
// attach this function to a button
public void functionToCallOnButtonClick()
{
pointer += 1;
if(pointer > alphabet.length - 1) // array start at 0, so need a -1
{
// do something about the fact you've run out of letters
}
alphaText.text = alphabet[pointer];
}
this might be of interest, it explains the “game loop” unity performs each frame, references to the UI are the old legacy OnGUI so that’s out of date, but it should help understand what happens each frame.