- Home /
Reload script not working
Hello dear people of Unityanswers. I got this script:
var Bullets = 32; var reloadTime = 4;
function Shoot()
{
if(canShoot)
{
var Bullet = Instantiate(Bullet,gameObject.Find("BulletSpawnPoint").transform.position,
Quaternion.identity);
Bullet.rigidbody.AddForce(transform.forward * 1000);
Bullets -= 1;
if(Bullets == 0)
{
canShoot = false;
WaitForSeconds(reloadTime);
canShoot = true;
Bullets = 32;
}
}
}
The Shoot function is called from function Update. I am having truble making my character reload when the bullet count is 0. It should be simple but i cant make it work. Can you guys help a fellow out? That would be great :)
Answer by The_r0nin · Jan 14, 2011 at 02:16 PM
var bullets:int = 32; var bulletPrefab: GameObject; var reloadTime:int = 4; var shotTime: float; var canShoot:boolean = true;
function Shoot() { if(canShoot) { var bullet = Instantiate(bulletPrefab, gameObject.Find("BulletSpawnPoint").transform.position, Quaternion.identity); bullet.rigidbody.AddForce(transform.forward * 1000); bullets -= 1; if(bullets <= 0) { shotTime = Time.time; canShoot = false; } } else { if ((Time.time - reloadTime) >= shotTime){ bullets = 32; canShoot = true; } } }
Try it this way. I didn't see where you declared "canShoot" outside of the Shoot() function, so that may have been your issue...
Answer by efge · Jan 14, 2011 at 09:12 AM
Maybe you should use something like this:
function Shoot() {
if (Bullets > 0) {
...
}
else {
yield WaitForSeconds(reloadTime);
Bullets = 32;
}
}
Your answer
Follow this Question
Related Questions
reload gun? simpel question 2 Answers
Function broken after Application.LoadLevel() 2 Answers
Gun with ammo help 1 Answer
Reload Script Not Working 1 Answer
Prefabs missing on reload 0 Answers