hi i am brand new to the forums and i am having trouble with my shooter script its meant to have a clip and when you shoot the clip runs out however the problem is that the clip gets reset to 10 over and over again i have tried many ways of fixing it but have not found a successful one any help would be great thank you. here is the code.438320–15234–$Shoot with reload.js (532 Bytes)
var BulletPrefab:Transform;
var ShootForce:float = 1000.0;
var Bullets:int= 10;
function Update() // This is called many times in second, for example in webplayer fps is capped to 60 so if you have no lag it is called 60 times in second.
{
if(Bullets == 0) // we have a situation that we have no bullets so...
{
reload(); // lets reload.
}
else
{
shoot();
}
}
function reload()
{
yield WaitForSeconds(5); // we wait 5 seconds.
Bullets = 10; //then add bullets.
// since we have yield here this is a [URL="http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html"]coroutine[/URL]. it is started in update and then forget, it will do it's stuff and then (hopefully) end
// Now while one instance of this reload is waiting 5 seconds, in optimal situation in webplayer update() is called 299 times more.
// so there is 299 instances or reload() running somewhere, each waiting 5 seconds then setting bullets to 10.
}
function shoot()
{
print(Bullets);
if(Input.GetButtonDown("Jump"))
{
gameObject.SendMessage("OnDamage", null);
var InstanceBullet = Instantiate(BulletPrefab, transform.position, transform.rotation);
InstanceBullet.rigidbody.AddForce(transform.forward * ShootForce);
Bullets -= 1;
}
}