- Home /
Pick Up Script "overflowing"
Hey guys I'm having and issue with my pick scripts. I have sit a "ammo limit" and set my pick up item to send a message to add an amount of ammo to the gun. Lets say my gun shoots 199 out of 200 ammo, then when I pick up my ammo it adds more then the 200 max. Like if I was at 199 it makes it 209 and then adds no more if I pick up more ammo. Here are my scripts.
the pick up box "pick up" #pragma strict
var ammoPickUp = 10;
function OnCollisionEnter (col : Collision) { col.gameObject.BroadcastMessage("ammoPickedUp", ammoPickUp, SendMessageOptions.DontRequireReceiver); Destroy(gameObject); }
An this is the Gun it self script
pragma strict
// shoot obj machineGun
var projectile : Transform; var bulletSpeed : float = 20; var damage = 10; static var ammoMG = 200;
function Update () { // Put this in your update function if (Input.GetButtonDown("MGfire")&& ammoMG > 0) {
// Instantiate the projectile at the position and rotation of this transform
var clone : Transform;
clone = Instantiate(projectile, transform.position, transform.rotation);
// Add force to the cloned object in the object's forward direction
clone.rigidbody.AddForce(clone.transform.forward * bulletSpeed);
}
if (Input.GetButtonDown("MGfire")&& ammoMG >0) {
ammoMG -= 1; } }
function ammoPickedUp (ammoPickUp : int) { if (ammoMG <= 200){ ammoMG += ammoPickUp; } }
function OnCollisionEnter (col : Collision) { col.gameObject.BroadcastMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver); }
function OnGUI () {
GUI.Label (Rect (20, 20, 100, 20),"MGammo:" + ammoMG ); }
Now I just can't see why the pick up massage over rides my max ammo count? Basically from what I can understand I'm send a "plus 10 message" and it adds 10, but its adding more then the maximum of 200. I so lost on how to make it that the ammo will only take what it needs to be 200 and no more.
Answer by Piflik · May 05, 2012 at 11:08 AM
This should work:
function ammoPickedUp(ammoPickUp : int) {
ammoMG += ammoPickUp;
if ammoMG > 200
ammoMG = 200;
}
You should only call the function, if the ammo counter is below 200, so the player cannot accidentally destroy ammo packs when he is already full.
Also, please format your code correctly next time.
Answer by Flynn · May 04, 2012 at 11:18 PM
function ammoPickedUp (ammoPickUp : int) { if (ammoMG <= 200){ ammoMG += ammoPickUp; } }
Let's walk through this.
If you have 199 bullets left, and you ask it to add ten, then first, it will check if 199 is below 200. 199 is. It will then add 10, resulting in 209 bullets.
To fix this, instead write your function like this:
function ammoPickedUp (ammoPickUp : int)
{
ammoMG += ammoPickUp;
ammoMG += 200 - ammoMG;
//if ammoMG is ten higheer than 200, 200-210 will result in negative ten. Adding a negative number causes it to go down by ten. Making it 200
}
Answer by VGeorgeV · May 06, 2012 at 02:06 AM
Thanks i missed with the input and came to the fallowing.
function ammoPickedUp(ammoPickUp : int) {
ammoMG += ammoPickUp;
if (ammoMG > 200) {
ammoMG = 200;
}
Answer by VGeorgeV · May 06, 2012 at 02:06 AM
All so sorry for the code formatting issue, It seems you gata post, stop, space then post code. I didn't space stuff out proper.
Your answer
![](https://koobas.hobune.stream/wayback/20220613071115im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
gui help for ammo display 1 Answer
skip guns with no ammo left 1 Answer
ammo cache pickup 1 Answer
Sleep Syntax Unity 1 Answer
Help Needed with Shotgun script (SOLVED) 0 Answers