help with ammo scripting

hey guys i am trying to make a zombie game but with robots and i am trying to make a script to that when i walk up to a gun on the wall there is a message saying how much it cost to reload and i can press “R” and it would give me more ammo.Can anyone plz help me i have been looking everywhere to find the answer. thanks you can email me at skully3100@gmail.com or just reply to this thanks.

i have three scripts:

var range = 100.0;
var fireRate = 0.05;
var force = 10.0;
var damage = 5.0;
var bulletsPerClip = 40;
var clips = 20;
var reloadTime = 0.5;
private var hitParticles : ParticleEmitter;
var muzzleFlash : Renderer;

private var bulletsLeft : int = 0;
private var nextFireTime = 0.0;
private var m_LastFrameShot = -1;

function Start () {
	hitParticles = GetComponentInChildren(ParticleEmitter);
	
	// We don't want to emit particles all the time, only when we hit something.
	if (hitParticles)
		hitParticles.emit = false;
	bulletsLeft = bulletsPerClip;
}

function LateUpdate() {
	if (muzzleFlash) {
		// We shot this frame, enable the muzzle flash
		if (m_LastFrameShot == Time.frameCount) {
			muzzleFlash.transform.localRotation = Quaternion.AngleAxis(Random.value * 360, Vector3.forward);
			muzzleFlash.enabled = true;

			if (audio) {
				if (!audio.isPlaying)
					audio.Play();
				audio.loop = true;
			}
		} else {
		// We didn't, disable the muzzle flash
			muzzleFlash.enabled = false;
			enabled = false;
			
			// Play sound
			if (audio)
			{
				audio.loop = false;
			}
		}
	}
}

function Fire () {
	if (bulletsLeft == 0)
		return;
	
	// If there is more than one bullet between the last and this frame
	// Reset the nextFireTime
	if (Time.time - fireRate > nextFireTime)
		nextFireTime = Time.time - Time.deltaTime;
	
	// Keep firing until we used up the fire time
	while( nextFireTime < Time.time && bulletsLeft != 0) {
		FireOneShot();
		nextFireTime += fireRate;
	}
}

function FireOneShot () {
	var direction = transform.TransformDirection(Vector3.forward);
	var hit : RaycastHit;
	
	// Did we hit anything?
	if (Physics.Raycast (transform.position, direction, hit, range)) {
		// Apply a force to the rigidbody we hit
		if (hit.rigidbody)
			hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
		
		// Place the particle system for spawing out of place where we hit the surface!
		// And spawn a couple of particles
		if (hitParticles) {
			hitParticles.transform.position = hit.point;
			hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
			hitParticles.Emit();
		}

		// Send a damage message to the hit object			
		hit.collider.SendMessageUpwards("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
	}
	
	bulletsLeft--;

	// Register that we shot this frame,
	// so that the LateUpdate function enabled the muzzleflash renderer for one frame
	m_LastFrameShot = Time.frameCount;
	enabled = true;
	
	// Reload gun in reload Time		
	if (bulletsLeft == 0)
		Reload();			
}

function Reload () {

	// Wait for reload time first - then add more bullets!
	yield WaitForSeconds(reloadTime);

	// We have a clip left reload
	if (clips > 0) {
		clips--;
		bulletsLeft = bulletsPerClip;
	}
}


function GetBulletsLeft () {
	return bulletsLeft;
}

var target : Transform;



function Update () {

var other = gameObject.GetComponent("Gun");



if ( Vector3.Distance(target.position, transform.position ) < 4) {

other.enabled = true;

}

if ( Vector3.Distance(target.position, transform.position ) > 4) {

other.enabled = false;

}

}

and this after that:

var bulletGUI : GUIText;



function OnGUI () {



GUI.Box(Rect(Screen.width /2 - 100,Screen.height /2 - 100,250,200),"250 POINTS CLICK R TO RELOAD");



}

if(Input.GetKeyDown( KeyCode.R ))

i dont know wat to do after that.

basically what you want is a on trigger event, so when you walk close enough to the wall a pop up message comes up ( OnGUI) and then also in the on trigger event you want it to have something along the lines of if the players pushes down the “R” key increase the ammo by whatever amount you want and then takes away however much money if he has the money

hope this helps :smiley:

Alright. This is two scripts bearing example code that should work or should give you the idea of how to do it:

Wallweaponscript.js

var moneyToSpend:int;
var clipsToGive:int;

function OnTriggerStay(other:collider){
if(other.gameObject.tag==player){
if(Input.GetKeyDown( KeyCode.R )&&other.GetComponent("MoneyHandlerScript").money>=moneyToSpend){
other.GetComponent("MoneyHandlerScript").money-=moneyToSpend;
other.GetComponent("YourWeaponScript").clips+=clipsToGive;
}
}
}

MoneyHandlerScript.js:

var money:int;
var startingMoney:int;

function Awake(){
money=startingMoney;
}
function OnGUI(){//we use this to show the current amount of money on the screen
GUI.Label(Rect(10,Screen.height-20,50,20),money);
}

How to use: You first create a trigger around the weapon on the wall and attach the Wallweapontrigger.js script there. Then, the money script to your character controller. Don't forget to tag the gameobject in which the money script is placed as "player" and replace "YourWeaponScript" with the actual name of your script.