- Home /
 
What's stopping 'WaitForSeconds' from working?
I have the script with the coroutine:
 float maxDuration = 0.5f;
     public IEnumerator slowDown = null;
     float countdown = 0;
 
 
     public IEnumerator SlowTime(float scale, float dur){
         if(dur > maxDuration){
             dur = maxDuration;
         }
         if(dur < 0){
             dur = 0.001f;
         }
 
         countdown = 0;
         Time.timeScale = scale;
         Debug.LogWarning("Scale: " + Time.timeScale + " - Duration: " + dur);
         yield return new WaitForSeconds(dur * Time.timeScale);
         Time.timeScale = 1;
     }
 
               and the script setting it ( where The final "Slow" function is probably the most relevant.):
     bool meleeDeflectionEnabled;
     bool projectileDeflectionEnabled;
     bool spellDeflectionEnabled;
     GameObject player = null;
     States playerState;
     GameObject enemy = null;
     public Vector3 deflectDirection;
     float distance = 1.5f;
     GameObject instancePrefab;
 
     float timeScale = 0.1f;
     float slowDuration = 0.5f;
     TimeControl timeControl;
 
     void Start(){
         instancePrefab = Resources.Load("DeflectionTarget",typeof(GameObject)) as GameObject;
         player = transform.parent.gameObject;
         //deflectDirection = player.GetComponent<CombatInput>().direction;
         playerState = player.GetComponent<States>();
         timeControl = GameObject.FindGameObjectWithTag("Vital").GetComponent<TimeControl>();
     }
 
     //This part is going to be called immediately, and only once, when something enters. I'm going to have to apply this script through script because of that...
     //since I can't .enabled = false the OnTriggerEnter part, only updates, which aren't in use.
 
     void OnTriggerEnter(Collider attack){
         //if player is deflecting and if collider is attack,
         CheckAttack(attack);
 
     }
 
     void CheckAttack(Collider attack){
         if(attack.tag == "Attack"){
             if( attack.GetComponent<DamageVolume>() != null){
                 AttackTypes attackType = attack.GetComponent<DamageVolume>().attackType;
                 if( attackType == AttackTypes.Melee){
                     enemy = attack.transform.parent.gameObject;
                     Deflection(attack.transform, deflectDirection);
 
                 }
             }
         }
     }
 
     void Deflection(Transform attack, Vector3 deflectDir){
         //What does deflection mean? It means the the opponent is pushed towards a target location, and facing a certain direction. 
         Slow();
         Vector3 deflectPosition = (deflectDirection * distance) + player.transform.position;
 //        Instantiate(instancePrefab, deflectPosition, Quaternion.identity);
 
         //Target location Have them both face it, and move toward it.
         //should I maybe handle the movement in the enemy AI? yeah.
         //where should the AI get the position data from? From the deflection volume? Sure.
 
         //enemy.transform.position
         enemy.GetComponent<EnemyAI>().deflectionPosition = deflectPosition;
         enemy.GetComponent<EnemyAI>().deflectedTime = enemy.GetComponent<EnemyAI>().defaultDeflectTime;
         //The attack is also displayed as nullified for the player.
         //stop damage of all attacks in those frames.
         //Get distance between attacker and target point, and have set the velocity so it happens over a set amount of seconds.
         //Rotate a set amount per second. Decided the the difference in degrees. 
         //Get damage data and reduce the damage. 
         //attack
         playerState.deflecting = false;
 
         Destroy(this);
     }
 
     void Slow(){
         //Check if stopping coroutine is necessary
         if( timeControl.slowDown != null){
             StopCoroutine(timeControl.slowDown);
         }
         //Set corouting data
         timeControl.slowDown = timeControl.SlowTime(timeScale, slowDuration);
         //start  the coroutine
         StartCoroutine(timeControl.slowDown);
         //Set buffers.
         timeScale *= 2;
         slowDuration *= 0.6f;
     }
 
               I'm extremely confused as to what's happening since I get feedback that the script has been entered, but slowdown doesn't always happen in a similar script - but this one in particular only slows down, and doesn't recover, as if the " yield return new WaitForSeconds(dur * Time.timeScale);" line either never starts or never resolves.
Answer by Grievemourne · Aug 12, 2015 at 06:55 PM
Answering my own question: I thought I got around the code getting killed when the script was destroyed , but I was calling the StartCoroutine in the script that was getting destroyed, (by using StartCoroutine(timeControl.slowDown);) instead of the TimeControl script ( with timeControl.StartCoroutine(timeControl.slowDown);)
Your answer