When I hit play, the debug logs should take 5 seconds to finish the countdown. Although, It doesn’t take a full 1 second, actually, just hitting play is enough to appear the entire and already finished countdown on the console.
What’s Wrong?
But that’s starting a new coroutine every frame.
That’s usually not desired.
And it does also not allow the effects object to ever be set disabled, as there’re always rountines that keep setting it active.
Speaking of the SetActive call in that loop - it’s enough to set it active once in the beginning (I’m aware you just copied his logic and pasted it - it’s rather small improvement for @FGPArthurVII ).
Tryed johne5’s answer, It did indeed work, but not for the second time.
Now for the real code: The intention is to switch between 2 models wich are going to perform a parade (like a fashion parade), one gets of the base, which projects a holographic like effect when it spawns and after some seconds it shuts off, the model does the parade, go back, that’s when he disappears and appears now the second model and follows the same fate as the last one. After both go, the cycle reapeats, and the first one appears and go, and the process start all over again infinitelly.
But when the first dude gets off scene, the effects do not turn on again, instead, they just keep turned off forever.
This is the first code, which is applyed to the catwalk, this one is responsible for spawning the models.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Parade : MonoBehaviour
{
public static GameObject newInstance;
public static bool next;
public GameObject start;
public GameObject dummy_1;
public GameObject dummy_2;
public GameObject effects;
private GameObject theOne;
private int counter;
private float fxTimer;
private int maxTime;
void Start ()
{
Debug.Log ("Starting Process");
counter = 0;
next = true;
maxTime = 3;
fxTimer = 0;
}
void Update ()
{
if (next == true)
{
CheckCounter ();
Instance ();
Debug.Log("Instanced");
}
}
private void CheckCounter ()
{
if (counter > 1)
{
counter = 0;
}
if (counter == 0)
{
theOne = dummy_1;
Debug.Log("Chosen 1");
}
else if (counter == 1)
{
theOne = dummy_2;
Debug.Log("Chosen 2");
}
}
void Instance ()
{
//SetEffects ();
StartCoroutine(SetEffects ());
newInstance = Instantiate (theOne, start.transform.position, start.transform.rotation);
next = false;
counter +=1;
}
// void SetEffects ()
// {
// for (float i = 0; i < maxTime + .1; i += Time.deltaTime)
// {
// if (i < maxTime)
// {
// effects.SetActive (true);
// Debug.Log ("Time" + i);
// }
// else
// {
// effects.SetActive (false);
// Debug.Log ("Effect Turned Off");
// }
// }
// }
IEnumerator SetEffects ()
{
Debug.Log ("Fx On");
while (fxTimer < maxTime)
{
effects.SetActive (true);
fxTimer += Time.deltaTime;
Debug.Log ("Time = " + fxTimer);
yield return null;
}
effects.SetActive (false);
Debug.Log ("Fx Off");
}
}
And this is the second code, that is applyed to the models (since they are the owners of the walking animation), this script is an event that is called everytime the walking through the catwalk reaches it’s last frame.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Toga : MonoBehaviour
{
void EndAnimation ()
{
Destroy (Parade.newInstance);
Parade.next = true;
}
}
Not sure, but I noticed that you set the effects active within your loop. This only needs to happen once outside of the loop.
Next, I don’t see where you reset the fxTimer, so you might want to set that to 0 before exiting the coroutine (outside + after the loop, I mean)