Code somehow skipping setting an object active

I know this isn’t a very important problem, but I’ve stared at this script for what feels like hours and cannot find why, when the wait comes up to wait3Active, it just skips it… For some referance, I’m disabling and re-enabling text elements. I’m in Unity 2018.4. The script is supposed to active an object for two seconds, deactivate it, and re-enable another one then start the next coroutine. If anyone knows how I could fix this, please let me know.

6072771–658500–C#ErrorScript.txt (1.61 KB)

I think you’re massively overcomplicating this… Why do you need 3 coroutines? Why does each coroutine run forever in an infinite loop? This will start new coroutines over and over again every two seconds. Try this:

void Start() {
    SecondLine.SetActive(false);
    ThirdLine.SetActive(false);
    FourthLine.SetActive(false);

    StartCoroutine(DoStuff(2.0f));
}

IEnumerator Dostuff(float waitTime) {
    FirstLine.SetActive(true);

    yield return new WaitForSeconds(waitTime);
    FirstLine.SetActive(false);
    SecondLine.SetActive(true);

    yield return new WaitForSeconds(waitTime);
    SecondLine.SetActive(false);
    ThirdLine.SetActive(true);

    yield return new WaitForSeconds(waitTime);
    ThirdLine.SetActive(false);
    FourthLine.SetActive(true);
}

Or better yet just put the lines in an array:

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

public class TextSwitcher : MonoBehaviour
{
    public GameObject[] Lines;

    void Start() {
        StartCoroutine(ShowLines(2.0f));
    }
   
    IEnumerator ShowLines(float waitTime) {
        // First disable them all
        foreach (GameObject line in lines) {
          line.SetActive(false);
        }

        // Now show each one for a short time in sequence
        for (int i = 0; i < Lines.Length; i++) {
          GameObject line = Lines[i];
          line.SetActive(true);
          yield return new WaitForSeconds(waitTime);
          line.SetActive(false);
        }
    }
}

Hey thanks so much for the help man! It really helped me continue my project and keep going! :smile: