Hey guys having some trouble getting a yield function to work in unity 5?

I simply want the projectile to have a delay once it has been fired so the animation for reload can be played?

Please Help?

var projectile : Rigidbody;

var speed = 10;
var attackReady : boolean;

function Update (){
	if (Input.GetButtonUp ("Fire1") && attackReady == (true))
	{
		clone = Instantiate (projectile, transform.position, transform.rotation);
		clone.velocity = transform.TransformDirection(Vector3 (0,0,speed));
		clone.AddForce (clone.transform.forward * speed);
	}
}

function attackReady () {
	if (Input.GetButtonUp ("Fire1") )
	{ 
		attackready = false;
		yield WaitForSeconds (3);
		attackready=true;
	}
}

Try this.

var projectile : Rigidbody;

var speed = 10;
var attackReady : boolean = true;

var reloadTime = 1;

function Update (){
	if (Input.GetButtonDown ("Fire1") && attackReady == (true))
	{
		clone = Instantiate (projectile, transform.position, transform.rotation);
		clone.velocity = transform.TransformDirection(Vector3 (0,0,speed));
		clone.AddForce (clone.transform.forward * speed);
		
		attackReady = false;
		Invoke("attackReady", reloadTime);
	}
}

function attackReady () {
	attackReady = true
}

Also I would like to suggest you watching Unity Tutorials. They are awesome.

To use a function as coroutine within UnityScript you have to yield the function you are calling.

This can be seen in example here: Coroutine - Unity Scripting API

So, to call the attackReady() function as a coroutine you would write the following:

     var projectile : Rigidbody;
     
     var speed = 10;
     var attackReady : boolean = true;
     
     function Update ()
     {
         if (Input.GetButtonUp ("Fire1") && attackReady == (true))
         {
             clone = Instantiate (projectile, transform.position, transform.rotation);
             clone.velocity = transform.TransformDirection(Vector3 (0,0,speed));
             clone.AddForce (clone.transform.forward * speed);

             yield attackReady();
         }
     }
     
     function attackReady () 
     {
         if (Input.GetButtonUp ("Fire1") )
         { 
             attackready = false;
             yield WaitForSeconds (3);
             attackready=true;
         }
     }

As has been noted by incorrect. You can use the Invoke function just fine and in your case it will be easier and cleaner.

Furthermore, your naming conventions are extremely impractical naming your boolean ‘attackReady’ along with naming your function using camelCase with the same name is an extremely confusing way to write your code (unless of course there is something happening that I’ve missed due to the UnityScript language). But, that is merely a suggestion. Hope it helps! :slight_smile: