Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DanielB89 · Oct 11, 2014 at 10:12 PM · pickuphudammohealth

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image 767_2 · Oct 11, 2014 at 10:14 PM 1
Share

you posted a long script , it is not a question , make it answerable

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges