- Home /
How can I keep my ammo count going over the cap?
I'm trying to keep the ammo from exceeding it's max count of 10/10 on the HUD.
using UnityEngine;
using System.Collections;
public class ShipPlayerController : MonoBehaviour {
// The max ammo the player start with
[SerializeField] int MaxAmmo = 10;
// The current ammo remaining for the player
private int CurAmmo = 10;
// The max health the player start with
[SerializeField] int MaxHealth = 3;
// The current lives remaining for the player
private int CurHealth = 3;
// Use this for initialization
void Start () {
ResetStats();
}
// Update is called once per frame
void Update () {
UpdatePosition();
UpdateFiring();
}
// Assign the "stat" variables their defualt values
void ResetStats() {
numShotsFired = 0;
numEnemyHits = 0;
// MaxAmmo is pulic and set in the editor
CurAmmo = MaxAmmo;
// MaxHealth is public and set in the editor
CurHealth = MaxHealth;
PlayerHUD.CreateHpIcons(MaxHealth);
// Tell the how many health icons should be active
PlayerHUD.updateHp(CurHealth);
// Tell the HUD to create Ammo count
PlayerHUD.updateAmmo(CurAmmo);
}
// Modify the active ammo count by passed value
public void ModAmmo(int _value) {
CurAmmo += _value;
// Update the new ammo value to the HUD
PlayerHUD.updateAmmo(CurAmmo);
}
public void ModHealth(int _value) {
CurHealth += _value;
// Tells the hud how many health icons should be active
PlayerHUD.updateHp(CurHealth);
// Handles the spawning of the projectile
void DoWeaponFire() {
print("The \"DoWeaponFire\" function has been called!");
// Create a new instance of the bullet and place it at the location
// of the player.
Instantiate(Bullet, transform.position, transform.rotation);
ModAmmo(-1);
}
void OnTriggerEnter(Collider other) {
// What type of object did we collide with?
if (other.tag == "Pickup")
{
if (other.name == "Score")
{
// Reward some points
ModScore(5);
// Remove the pickup from the game
Destroy(other.gameObject);
}
if (other.name == "AmmoPickup") {
// Reward player with ammo
ModAmmo(3);
// Remove the pickup form the game
Destroy(other.gameObject);
}
}
// wrecked into an enemy?
else if (other.tag == "EnemyShip") {
ModLives(-1);
ModHealth(-1);
ModShield(-25);
}
}
}
Another issue I'm having is that the health does not update on the HUD when the player ship collides with the enemy but everything else from the HUD does. Never mind I fixed the problem with the HUD not updating the health
you posted a long script , it is not a question , make it answerable
Answer by NoseKills · Oct 11, 2014 at 10:56 PM
You are not limiting the ammo count in any way so of course it will go over whatever number
// Modify the active ammo count by passed value
public void ModAmmo(int _value) {
CurAmmo += _value;
// if you want to cap your ammo count, then... cap it
if (CurAmmo > MaxAmmo)
CurAmmo = MaxAmmo;
// Update the new ammo value to the HUD
PlayerHUD.updateAmmo(CurAmmo);
}
As for your second question about health... i really cant answer because i have no idea what PlayerHUD.updateHp(CurHealth);
does
Your answer
![](https://koobas.hobune.stream/wayback/20220613164908im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Health Pickup is Not Working 1 Answer
Add Health on Pickup to Decaying Health,Make a collision with an object add health 2 Answers
eating candy, spitting it out. 1 Answer
Making an HUD 3 Answers
ammo cache pickup 1 Answer