I want two loops running alternatively (1 second) to make an if statement for a players death.
I just need to know how to make alternating loops to implement a death
Example:
Loop 1 / Loop 2 / Loop 1 / Loop 2 / Loop 1 / Loop 2 …
I don’t think there is a way to run two loop at once but you could try this :
bool loop = true ;
while (true) // loop
{
if (loop == true) ``
{
//loop 1
}
else
{
//loop 2
}
loop = !loop; // if loop is true than it will change it to not true so false and vica versa
}
Hi, what do you mean with two loops? Two loops that run only once per frame (like Update) or a while loop? If it is a while loop it will probably crash if you would like to run it for one second.
Anyway I created this that works as a Update Loop and change Loop after 1 second (similar to @Reun_'s solution)
public bool Loop1;
public float MaxTime;
public float CurrentTime;
void Start()
{
Loop1 = true;
MaxTime = 1;
CurrentTime = 0;
}
void Update()
{
if (CurrentTime > MaxTime)
{
Loop1 = !Loop1;
CurrentTime = 0;
}
CurrentTime += Time.deltaTime;
if (Loop1 == true)
{
//Do Something in Loop 1
}
if (Loop1 == false)
{
//Do Something in Loop 2
}
}
And here a solution with Coroutines:
void Start()
{
StartCoroutine(Loop1());
}
void Update()
{
}
IEnumerator Loop1()
{
float MaxTime = 1;
float CurrentTime = 0;
while (CurrentTime < MaxTime)
{
//Do Something...
CurrentTime += Time.deltaTime;
yield return null;
}
StartCoroutine(Loop2());
yield return null;
}
IEnumerator Loop2()
{
float MaxTime = 1;
float CurrentTime = 0;
while (CurrentTime < MaxTime)
{
//Do Something...
CurrentTime += Time.deltaTime;
yield return null;
}
StartCoroutine(Loop1());
yield return null;
}