4
\$\begingroup\$

In the code below, I have a cycle for enemies appearing and disappearing.

IEnumerator EnemyCycle()    {
while (isRepeating)
{

for (int j = 0; j < enemies.Length; j++) {

    canStartUpdatingReset = true;
    Enemy currentEnemy = enemies [j];
    var _myMaterial = currentEnemy.GetComponent<Renderer>().material;
    var _currentFade = StartCoroutine(FadeToForEnemy(_myMaterial, 0f, fTime, currentEnemy.gameObject, false));

coroutinesToStop.Add(_currentFade);

if (currentEnemy.hasWeapon) {
                weaponCycleCoroutines.Add(StartCoroutine(FadeToForWeapon(currentEnemy.weapon.GetComponent<Renderer>().material, 0f, fadeTime, currentEnemy.weapon, false)));

            }
}

yield return new WaitForSeconds (hideTime);

for (int j = 0; j < enemies.Length; j++) {

    Enemy currentEnemy = enemies [j];
    var _myMaterial = currentEnemy.GetComponent<Renderer>().material;
    var _currentFade = StartCoroutine(FadeToForEnemy(_myMaterial, 1f, fTime, currentEnemy.gameObject, true));

     coroutinesToStop.Add(_currentFade);
if (currentEnemy.hasWeapon) {
weaponCycleCoroutines.Add(StartCoroutine(FadeToForWeapon(currentEnemy.weapon.GetComponent<Renderer>().material, 1f, fadeTime, currentEnemy.weapon, true)));
}
yield return new WaitForSeconds (showTime);

        }
    }
}

I have an enemyCycleDuration float which is

enemyCycleDuration = 60*(hideTime + fadeTime + showTime + fadeTime)

Note that fadeTime comes from FadeToForWeapon and FadeToForEnemy. The idea here is I want the enemyCycleDuration to be run in parallel to EnemyCycle() so it is reset (please see code below) at the same time that

yield return new WaitForSeconds (showTime);

is reached in the EnemyCycle().

I am resetting enemyCycleDuration in the following way in the update method but EnemyCycle() seems to be ahead of enemyCycleDuration, always completing before enemyCycleDuration is reset. How can I get both to run parallel to each other in timing and complete at the same time?

if (canStartUpdatingReset)  {
timeElapsed +=1;
if(timeElapsed >= enemyCycleDuration) {

    timeElapsed = 0;

    Debug.Log ("Reset CycleDuration");
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ Can someone please help me \$\endgroup\$ Commented Jul 24, 2017 at 5:46

1 Answer 1

1
\$\begingroup\$

What is the variable "hideTime"? I don't see where the value of it is being determined.

If I'm understanding your code correctly; coroutines are started/stopped in "EnemyCycle()" here:

while (isRepeating)
{

for (int j = 0; j < enemies.Length; j++) {

    canStartUpdatingReset = true;
    Enemy currentEnemy = enemies [j];
    var _myMaterial = currentEnemy.GetComponent<Renderer>().material;
    var _currentFade = StartCoroutine(FadeToForEnemy(_myMaterial, 0f, fTime, currentEnemy.gameObject, false));

coroutinesToStop.Add(_currentFade);

if (currentEnemy.hasWeapon) {
                weaponCycleCoroutines.Add(StartCoroutine(FadeToForWeapon(currentEnemy.weapon.GetComponent<Renderer>().material, 0f, fadeTime, currentEnemy.weapon, false)));

            }
}

What determines the value of this "isRepeating" loop? It looks like the routines started and stopped while nothing is delaying the loop. That leaves the two timers

yield return new WaitForSeconds (hideTime);

and

 yield return new WaitForSeconds (showTime);

These two, combined being the total time the cycle will run. Then you have the cycle duration variable which is 60 * those two timers and the additional values:

enemyCycleDuration = 60*(hideTime + fadeTime + showTime + fadeTime)

It's very possible I'm misunderstanding your code, which if I am, please let me know where I went wrong. But it looks like the time for the other cycle, EnemyCycleDuration will always be drastically higher then the EnemyCycle() routine.

You might try the "WaitWhile" yield statement in place of "WaitForSeconds", then you can simply tell it to wait until "EnemyCycleDuration" meets the the desired value like this:

yield return new WaitWhile (() => EnemyCycleDuration >= [some duration here]);

If you want it the other way around, you can change how EnemyCycleDuration is calculated that way your "CanStartUpdating" loop completes sooner.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I went to "Unanswered" and didn't see the date on this. I'm assuming it's probably already solved since 3 years ago lol. Either way, maybe this helps someone. I'm also not sure if there is anything against post necromancy here, I couldn't find any rules related to it \$\endgroup\$ Commented Apr 27, 2021 at 16:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .