IEnumerator while loop for charge weapon doesn't work...

The chargeDmg float doesn’t accumulate while fire-button is held down. I’ve tried editing the IEnumerator different ways and the only way I can get it to work is this way, but am I just returning null instead of the time.Deltatime value to chargeDmg? Is it more efficient in C# to separate methods that must happen simultaneously at run-time into events, members, properties, etc.? I’ve just started coding so I don’t think I"m quite there yet…

using UnityEngine;
    using System.Collections;
  
  
  
    public class CocoAndGlennAttacksIII : MonoBehaviour {
      
        Animator anim;
  
        public bool isAttacking = false;
      
        public Rigidbody fireball1;
        public Rigidbody iceball1;
        public Rigidbody fireball2;
        public Rigidbody iceball2;
      
        public float speed = 10;
      
      
        public bool attackVectorEast = false;
        public bool attackVectorNorth = false;
        public bool attackVectorWest = false;
        public bool attackVectorSouth = false;
      
  
      
        //public float triggerDownTime = 0;
        public float chargeDmg = 0;
        public float chargeTime = 2;
      
        public bool isCharging = false;
      
        Rigidbody clone;
      
  
      
      
        // Use this for initialization
        void Start () {
            anim = GetComponent<Animator>();
  
          
          
        }
      
  
        void Update () {
  
          
            float lastInputX = Input.GetAxis ("Horizontal");
            float lastInputZ = Input.GetAxis ("Vertical");
          
            if (lastInputX  > 0){ attackVectorEast = true;
                attackVectorWest = false; attackVectorNorth = false;attackVectorSouth = false;
            }
            else if (lastInputX < 0){ attackVectorWest = true;
                attackVectorEast = false; attackVectorNorth = false;attackVectorSouth = false;
            }
            else if (lastInputZ > 0) {attackVectorNorth = true;
                attackVectorWest = false; attackVectorEast = false;attackVectorSouth = false;
            }
            else if (lastInputZ < 0){ attackVectorSouth = true;
                attackVectorWest = false; attackVectorNorth = false;attackVectorEast = false;
            }
  
  
          
            if (Input.GetButtonDown("Fire1")){
                AttackCharger();
                isAttacking = true;
                anim.SetBool ("isAttacking", true);
              
                if(!isCharging){
                    isCharging = true;
  
  
  
                }
            }
  
          
                if (Input.GetButtonUp("Fire1"))
                {
  
  
                    if (attackVectorEast == true){
                        anim.SetBool ("isAttacking", true);
                        clone = Instantiate (fireball1, new Vector3(transform.position.x + 2.5f, transform.position.y, transform.position.z-0.5f), transform.rotation) as Rigidbody;
                        clone.velocity = transform.TransformDirection(Vector3.right * speed);
                    Physics.IgnoreCollision( clone.collider, transform.root.collider );
                      
                        Destroy (clone.gameObject, 2);
                        clone = Instantiate (iceball1, new Vector3(transform.position.x+ 3f, transform.position.y, transform.position.z -0.5f), transform.rotation) as Rigidbody;
                        clone.velocity = transform.TransformDirection(Vector3.right * speed);
                    Physics.IgnoreCollision( clone.collider, transform.root.collider );
                      
                        Destroy (clone.gameObject, 2);
                    chargeDmg = 0;
                    }
                    if (attackVectorWest == true){
                        anim.SetBool ("isAttacking", true);
                        clone = Instantiate (fireball1, new Vector3(transform.position.x - 3f, transform.position.y, transform.position.z-0.5f), transform.rotation) as Rigidbody;
                        clone.velocity = transform.TransformDirection(Vector3.left * speed);
                    Physics.IgnoreCollision( clone.collider, transform.root.collider );
                      
                        Destroy (clone.gameObject, 2);
                        clone = Instantiate (iceball1, new Vector3(transform.position.x+ -2.5f, transform.position.y, transform.position.z -0.5f), transform.rotation) as Rigidbody;
                        clone.velocity = transform.TransformDirection(Vector3.left * speed);
                    Physics.IgnoreCollision( clone.collider, transform.root.collider );
                      
                        Destroy (clone.gameObject, 2);
                    chargeDmg = 0;
                  
                    }
                    if (attackVectorNorth == true){
                        anim.SetBool ("isAttacking", true);
                        clone = Instantiate (fireball2, new Vector3(transform.position.x- 0.7f, transform.position.y, transform.position.z +1f), transform.rotation) as Rigidbody;
                        clone.velocity = transform.TransformDirection(Vector3.up * speed);
                    Physics.IgnoreCollision( clone.collider, transform.root.collider );
                        Destroy (clone.gameObject, 2);
                        clone = Instantiate (iceball2, new Vector3(transform.position.x+ 0.7f, transform.position.y, transform.position.z +1f), transform.rotation) as Rigidbody;
                        clone.velocity = transform.TransformDirection(Vector3.up * speed);
                    Physics.IgnoreCollision( clone.collider, transform.root.collider );
                      
                        Destroy (clone.gameObject, 2);
                    chargeDmg = 0;
                    }
                    if (attackVectorSouth == true){
                        anim.SetBool ("isAttacking", true);
                        clone = Instantiate (fireball2, new Vector3(transform.position.x- 0.7f, transform.position.y, transform.position.z -2.5f), transform.rotation) as Rigidbody;
                        clone.velocity = transform.TransformDirection(Vector3.down * speed);
                    Physics.IgnoreCollision( clone.collider, transform.root.collider );
                        Destroy (clone.gameObject, 2);
                        clone = Instantiate (iceball2,  new Vector3(transform.position.x+ 0.7f, transform.position.y, transform.position.z -2.5f), transform.rotation) as Rigidbody;
                        clone.velocity = transform.TransformDirection(Vector3.down * speed);
                    Physics.IgnoreCollision( clone.collider, transform.root.collider );
                      
                        Destroy (clone.gameObject, 2);
                    chargeDmg = 0;
                  
                  
                  
  
                }
          
              
  
              
                isAttacking = false;
                anim.SetBool ("isAttacking", false);
              
  
                isCharging = false;
                chargeDmg = 0;
  
          
            }
  
        }
  
     IEnumerator AttackCharger(){
            while(Input.GetButton ("Fire1")){
        if (chargeDmg < 10 ) {
                chargeDmg += chargeTime*Time.deltaTime;
            yield return null;
                }
  
    }
        }
  
  
    }

You need to start a Coroutine method using StartCoroutine(MethodName()) or StartCoroutine(“MethodName”)

Not entirely sure if that’s the issue, though.

Tried it and unity crashes if you’re holding the fire-button down right before chargeDmg reaches 10. I tried adding StopCoroutine after the IEnumerator is called in Update and it still crashes. Do I have to tell it to stop doing something? Does it have something to do with “yield”? Does returning “null” cause it to crash?

IEnumerator AttackCharger()
    {

        while(Input.GetButton ("Fire1"))
        {
            if (chargeDmg < 10 )
            {
                chargeDmg += chargeTime*Time.deltaTime;
            }

            yield return null;
        }
    }

This should fix it. Not an expert on coroutines, but I suspect this was crashing because the yield statement was unreachable once the chargeDmg variable reached 10+.

Incidentally, I don’t think the coroutine is executed at all unless StartCoroutine is used.

… why not just get time.time on button down. Then on button up grab the time again… compare the 2. Set your damage or whatever based on it.

getupkidak: it worked! god, that was so simple! thank you!

novashot: what the problem seemed to be is that the charging would stop at the beginning, or it would accumulate each time you tap and release the button until reaching maximum, rather than letting you hold the button down to charge. now it works exactly as it should, like in r-type!