Reloading Script

I am creating a projectile shooting script. I have no compile errors, but I encountered some problems after I added the reloading part. The gun will not fire anymore and I was wondering if some of you know what the problem is. Thank you in advance (btw Im using a seperate script on a bullet projectile that is spawned using this script).

var MaxAmmo =  1;
var Ammo = MaxAmmo;
var Bullets = 15;

private var timer = 0.0;
var reloadTime = 10.0;
 
var Fire : String = "MusketFire";
 
function Start () 
{
 
}
 
function Update () 
{
    if(Fire == "MusketFire")
    {
       if(Ammo > 0)
       {
         if(Input.GetMouseButtonDown(0))
         {
         FireOneBullet();
         }
       }
    }
 
 
    if(Input.GetKey("r"))
    {
       Reload();
   
    }
}
 
function FireOneBullet ()
{
    var Bullet = Instantiate(Bullet, FirePoint.transform.position, transform.rotation);
    Ammo --;
}
 
function Reload ()
 
{
    if(Ammo > MaxAmmo && Bullets > 0)
    { 
       if(timer<reloadTime)
       {
         timer+=Time.deltaTime;
       }
       else
         timer=0.0; 
 
    }
    if(timer>=reloadTime)
    	{
       		Ammo ++;
       		Bullets --;
    	} 
}

Any ideas?

To do some debugging you could change the first if-clauses in your update function to something like this: if(Input.GetMouseButtonDown(0)) { Debug.Log(Fire); if(Fire == "MusketFire") { Debug.Log(Ammo); if(Ammo > 0) { Debug.Log("Fire Bullet"); FireOneBullet(); } } } Then you can see if it has to do with any of those variables.

Did you try the debug logging? Does the FireOneBullet function get called?

No it does not :( Fire is, but "FireOneBullet isn't

So Fire gets logged but Fire Bullet doesn't. Does Ammo get called and what gets logged?

2 Answers

2

I think this is wrong:

function Reload (){
  // This won't happen since you have Ammo = MaxAmmo at the top
  if(Ammo > MaxAmmo && Bullets > 0) 
  {
    if(timer<reloadTime)
    {
        timer+=Time.deltaTime;
    }
    else
        timer=0.0;     
  }
  if(timer>=reloadTime)
  {
    Ammo ++;
    Bullets --;
  }
}

timer is not increasing since you would have to hold the button down.

I think you are willing to use coroutine as such:

function Reload (){
  if(Bullets > 0) // Not sure what you are checking here though
  {
    while(timer<reloadTime)
    {
        timer+=Time.deltaTime;
        yield;
    }
    timer=0.0;     
    Ammo ++;
    Bullets --;
  }
}

Unfortunately this didn't solve my problem, but thank you for pointing out the mistake! Since in my game, the player is using a musket, bullets is equal to magazines in a normal first person shooter. When your out of bullets, you can no longer reload the gun, basically.

at line 45, your using a classic wrong arrow typo. when is Ammo ever more than Max Ammo?
your timer never starts, and never finishes, also you never reset your timer after the actual reloading, so the user has to press R twice the next time around.
the largest error is that your timer is called only one per frame so you need to hold down are for 10 seconds before reloading will work.

got it working though:

var MaxAmmo =  1;
var Ammo = MaxAmmo;
var Bullets = 15;

var Bullet : GameObject;
var FirePoint : GameObject;
 
private var timer = 0.0;
var reloadTime = 10.0;
 
var Fire : String = "MusketFire";
var reloading : boolean;
 
function Start () 
{
 
}
 
function Update () 
{
    if(Fire == "MusketFire")
    {
        if(Ammo > 0)
        {
            if(Input.GetMouseButtonDown(0))
            {
                FireOneBullet();
            }
        }
    }
 
 
    if(Input.GetKey("r"))
    {
        Reload();
 
    }
    if(reloading) {
        if(timer<reloadTime)
        {
            timer+=Time.deltaTime;
        }
        else
            timer=0.0; 
 
        if(timer>=reloadTime)
        {
            Ammo ++;
            Bullets --;
            timer=0.0;
            reloading = false;
        } 
    }
}
 
function FireOneBullet ()
{
    var Bullet = Instantiate(Bullet, FirePoint.transform.position, transform.rotation);
    Ammo --;
}
 
function Reload ()
 
{
    if(Ammo < MaxAmmo && Bullets > 0)
    { 
        reloading = true;
    }


    
}

though 10 seconds for a reload time is pretty excessive.
hope it helps

"you need to press R hundreds of times", actually not really since he uses GetKey, he would have to keep pressing non-stop.

I stand corrected, i just saw "Input" then skipped past it

Okay thanks for the help guys I appreciate you taking the time to help a noob out. Unfortunately the bullet still doesn't instantiate even after I tried your script. On the other hand, the reloading works perfectly!

did you assign the bullet? did you assign Firepoint? if not there's your problem, if there are and still no resuts. try Firepoint.transform.rotation the ammo decreases so your Instantiate must be at fault

Yes they are. Im not quite sure if I get what you mean with that could you demonstrate what you want to do in a small script?