Update () can not be a coroutine (Assault Rifle fire rate)

Hi i’m currently working on a script for the assault rifle firerate in my game but i can’t test it properly because i get this error, but i have no idea what it means. I’m not exactly the most experienced coder.

Here’s my code… Please tell me what i’ve done wrong…

var Damage : int = 50 ;
var GunSound : AudioClip[];
var Blood : GameObject;
var Blood_splat : GameObject;
var Bullet_Hole : GameObject ;
var Ricochet : GameObject ;
var FireRate : float = 0.25 ;
var Canshoot = true ;

function Update ()
{
    if (Input.GetButton("Fire1"))
    {
    if (Canshoot== true) ;
    {
        var hit : RaycastHit;
        audio.clip = GunSound[Random.Range(0, GunSound.length)];
        audio.Play();
        if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
        {
        hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);   
        if ( hit.collider.gameObject.tag == "Flesh" )
        {
        Instantiate(Blood, hit.point, Quaternion.identity);
        Instantiate(Blood_splat, hit.point, Quaternion.identity);
        }
        if ( hit.collider.gameObject.tag == "World" )
        {
        Instantiate(Ricochet, hit.point, Quaternion.FromToRotation(Vector3.forward ,hit.normal));
        Instantiate(Bullet_Hole, hit.point, Quaternion.FromToRotation(Vector3.up ,hit.normal));
        }
        Canshoot = false ;
        }
       
    }
    if (Canshoot == false)
    {
    yield WaitForSeconds(FireRate);
    Canshoot = true;
    }
}
}

I’m not a UnityScript guy, but I know the yield statement in your Update is not allowed to be there. Your Update() must run every frame. It cannot wait for seconds or wait for another function to complete.

The way to start a Coroutine is a bit different in UnityScript than C# so I’ll let someone else explain how to do that.

This is how you might want to try structuring your script:

Modified Script

#pragma strict

var Damage : int = 50 ;
var GunSound : AudioClip[];
var Blood : GameObject;
var Blood_splat : GameObject;
var Bullet_Hole : GameObject ;
var Ricochet : GameObject ;
var FireRate : float = 0.25 ;
var Canshoot = true ;

function Shoot()
{
    var hit : RaycastHit;
  
    Canshoot = false;
  
    audio.clip = GunSound[Random.Range(0, GunSound.length)];
    audio.Play();

    if (Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
    {
        hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
      
        if ( hit.collider.gameObject.tag == "Flesh" )
        {
            Instantiate(Blood, hit.point, Quaternion.identity);
            Instantiate(Blood_splat, hit.point, Quaternion.identity);
        }

        if ( hit.collider.gameObject.tag == "World" )
        {
            Instantiate(Ricochet, hit.point, Quaternion.FromToRotation(Vector3.forward ,hit.normal));
            Instantiate(Bullet_Hole, hit.point, Quaternion.FromToRotation(Vector3.up ,hit.normal));
        }
      
    }
  
    yield WaitForSeconds(FireRate);
    Canshoot = true;
}

function Update ()
{
    if (Input.GetButton("Fire1") && Canshoot)
         StartCoroutine(Shoot());
}
1 Like