Reload while whole magazine is full

Guys im having a problem that i cant solve in my script :frowning:
its bassicly that i am able to reload while my whole magazine is full do you guys know how to fix it? i would really apreciate it :slight_smile: this is my script :

#pragma strict

var Range : float = 1000;
var Force : float = 1000;

var hitMark : GameObject;

var texta : GUITexture;
var four : Texture;
var three : Texture;
var two : Texture;
var one : Texture;
var zero : Texture;
var five : Texture;

var bullets : int = 5;

var shootSound : AudioClip;
function Start () {

}

function Update () {

if(Input.GetKeyDown(KeyCode.R))  {
	
	
	GameObject.Find("Gun").animation.Play("reload");
	

}

if(bullets == 5){ texta.guiTexture.texture = five; }

		else if(bullets == 0){ GameObject.Find("Gun").animation.Play("reload"); bullets = 5; }
		else if(bullets == 4){ texta.guiTexture.texture = four; }
		else if(bullets == 3){ texta.guiTexture.texture = three; }
		else if(bullets == 2){ texta.guiTexture.texture = two; }
		else if(bullets == 1){ texta.guiTexture.texture = one; }
	
	
	
	var hit : RaycastHit;
			
	var direction : Vector3  = transform.TransformDirection(Vector3.forward);
     
	Debug.DrawRay(transform.position , direction * Range , Color.blue);
			
		if(Input.GetMouseButtonDown(0)){
		audio.PlayOneShot(shootSound);			
		bullets--;
			
			if(Physics.Raycast(transform.position , direction , hit, Range)){
			
						var hitRotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
				
						if(hit.collider.gameObject.tag == "zombie"){
			
						    //hit.rigidbody.AddForceAtPosition( direction * 100 , hit.point);
						    //hit.collider.gameObject.animation.Play("die");
						    Instantiate(hitMark);
						    hit.collider.gameObject.SendMessage("ApplyDamage", 25);

							
					    }
			
			}
		}			

}

Erm…

You basically don’t have a reload function. Your script just instantly gives you 5 bullets whenever you run out. The only thing in this script associated with the reload is the sound.

Try putting this into your script: (and remove stuff from your code that are the same as mine)

var reloading : boolean = false;
var reloadTime : float = 2.2;
function Update(){
	if(Input.GetKeyDown(KeyCode.R) && bullets < 5 && !reloading)  {
		StartCoroutine("Reload");
	}
}
function Reload () {
	reloading = true;
    GameObject.Find("Gun").animation.Play("reload");
	yield WaitForSeconds (reloadTime);
	bullets = 5;
	reloading = false;
}

Hello FrreStuff,

Shouldn’t be too difficult to do. Basically, when you call reload, you need to do a check on how many bullets are in the gun. That way, if the gun is full, the condition isn’t satisfied and the reload will not take place.

 if(Input.GetKeyDown(KeyCode.R))  
 {
     if(bullets < 5)
     {
           GameObject.Find("Gun").animation.Play("reload");
     }    
 }

Unless you are doing it elsewhere, you will also need to set the bullets to 5.

Edit: If you are planning on having multiple guns, you might want to have a var for maxAmmo, and check if the gun has max ammo, rather than checking for a particular value. This will make your code more reusable!