Skipping the first ienumerator call

here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class StoryManager : MonoBehaviour
{
    public Text[] convos;
    public Button next;
    private int currentConvo = 0;
    public float delay;
    private bool endOfPage = false;
    private bool endOfConvo = false;


    // Start is called before the first frame update
    void Start()
    {
        endOfPage = false;
        endOfConvo = false;

        ChangeText();
    }

    public void ChangeText()
    {
        Debug.Log("current convo at the start " + currentConvo);

        Type();

        if(currentConvo == convos.Length - 1)
        {
            endOfPage = true;
        }
        if(endOfPage)
        {
            next.gameObject.SetActive(false);
        }

        currentConvo++;
        Debug.Log("current convo at the end " + currentConvo);
    }

    public void Type()
    {
        StartCoroutine(Typewriter());
    }

    IEnumerator Typewriter()
    {
        for(int i = 0; i <= convos[currentConvo].text.Length; i++)
        {
            Debug.Log("current convo during type " + currentConvo);
            GetComponent<Text>().text = convos[currentConvo].text.Substring(0,i);

            yield return new WaitForSeconds(delay);

            Debug.Log("here");
            if(i == convos[currentConvo].text.Length)
            {
                endOfConvo = true;
                Debug.Log("and here");
            }
        }
    }

    public void NextConvo()
    {
        if(!endOfConvo)
        {
            StopCoroutine(Typewriter());
            GetComponent<Text>().text = convos[currentConvo].text;
            endOfConvo = true;
        }
        if(endOfConvo)
        {
            currentConvo++;
            ChangeText();
        }
    }
}

for some reason, when the method is called and runs down to the typewriter coroutine, it debugs that it enters with currentconvo as 0, but doesn’t debug here, or the next debug, and here. Instead, it goes straight into the following currentconvo, and begins to typewrite out the second paragraph instead. THAT coroutine does it letter by letter, and debugs all the way through, then stops like it should. I cannot figure out why it doesn’t even remotely START typing out the first paragraph, and instead skips to the next. Also, when I hooked NextConvo() up to a button, and pressed it, it skipped the third paragraph in the same way as it does the first, then displays the forth paragraph. Any help welcome.

I think the problem is in ChangeText it starts the Coroutine does a few checks then increments the currentConvo right away skipping to the next paragraph and then the coroutine continues on with the next paragraph.

do you know of another section that I could separate the currentConvo++ to? I’ll work on it in the meantime, but in case I don’t figure a solution, any help with it is welcome.

I think just remove the currentConvo++ from ChangeText you only want it to increment when the user invokes NextConvo correct?

Yes! Thank you, I clearly misunderstood when to use int++ for my loops. It works perfectly now, much appreciated!

Yeah the coroutines are very hard to understand at first, glad you got it solved!

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms