What's wrong with this string list code?

Im trying to change the context of a Text UI Element by cycling through a string index each time the player hits space, using this:

110951-code.png
Supposedly, it should change the text two times and stop at the last one, waiting for C to change the whole scene.

Right now, it’s not even changing. It keeps rewriting the first string “text1” over and over. I can see “text2” trying to appear when I press space, but it goes right back to the first one.

What’s is happening?

Because you reset the index and declare your list each frame, your text will never be set to text2 or text3. You have to declare your index and your list as class members instead of local variables.

Moreover, read your code carefully, and you will see that you are trying to detect a keypress on C the same frame you try to detect a keypress on the spacebar, which is almost impossible.

public class YourClass : MonoBehaviour
{
    public List<string> Texts = new List<string>( "text1", "text2", "text3" };
    public Text UiText ;
    private int currentTextIndex = 0 ;

    private void Start()
    {
         if( currentTextIndex < Texts.Count )
             UiText.text = Texts[currentTextIndex] ;
    }

    private void Update()
    {
        if( Input.GetKeyDown( KeyCode.Space ) )
        {
            currentTextIndex++ ;
             if( currentTextIndex < Texts.Count )
                 UiText.text = Texts[currentTextIndex] ;
        }
        else if( currentTextIndex >= Texts.Count && Input.GetKeyDown( KeyCode.C ) )
        {
             currentState = states.c_1;
         }
    }
}

PS : Please, next time, post your actual code, not a screenshot of it.