• 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 handsomemuffin · Jul 09, 2012 at 04:15 PM · gui

GUI Ammo Count not counting down when I fire

Hi, I'm following a tutorial on how to make GUI's and HUD's. The guy who made it missed out an important piece of info. I know how to make the GUI show my initial ammo count (When I pick up the grenades) but when I fire the number won't lower.

These are the 2 codes I am using static var GRENADE_AMMO = 0;

function OnControllerColliderHit(hit: ControllerColliderHit)

{

if(hit.gameObject.tag == "crateGrenades")

{

//destroy the ammo box

Destroy(hit.gameObject);

//add ammo to inventory

GRENADE_AMMO += 8;

print("YOU NOW HAVE "+ GRENADE_AMMO +" GRENADES!");

GameObject.Find("g_Count").guiText.text = ""+GRENADE_AMMO;

}

}

and then

var speed = 3.0; var grenadePrefab:Transform; function Update () { //finding out if the fire button is pressed if(Input.GetButtonDown("Fire1")) { if(Collisions.GRENADE_AMMO > 0) {

//create the prefab var grenade = Instantiate(grenadePrefab, transform.position, Quaternion.identity);

//add force to prefab grenade.rigidbody.AddForce(transform.forward * 2000);

Collisions.GRENADE_AMMO --; print("YOU NOW HAVE "+ Collisions.GRENADE_AMMO +" GRENADES!"); } } }

What would I need to add to get this to remove 1 count from my GUI? Any help would be appreciated, only just started coding and its taking a bit to get my head around it xD

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Muuskii · Jul 10, 2012 at 12:56 AM

It seems your using

GameObject.Find("g_Count").guiText.text = ""+GRENADE_AMMO;

To update the guitext when you pick up ammo, but then don't update the text again when you fire a grenade.

btw why are you using COLLISIONS.GRENADE_AMMO > 0 to check if the player has any ammo left? Shouldn't that be a local variable?

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
avatar image
0

Answer by Chimera3D · Jul 09, 2012 at 11:04 PM

I see what you are trying to do, but it is a bit confusing to read your code. You should format your code better. Select all of your code then hit the binary code button while editing to format everything. Anyhow, I don't see anything being done as far as GUI goes, just printing. Are you using GUI textures or text? Are these game objects or code generated GUI elements? With that being said, if you are using GUI textures for each grenade icon on the screen just say whenever you throw a grenade, disable the next GUI texture over. To do this you can use an array or just compare the number of remaining grenades to determine what objects should be disabled. I would prefer the latter if you are a beginner.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Card matching game 1 Answer

How to put a GUI in a clickable Gameobject? (with or without using assest IGUI basic) 0 Answers

How do i cal second question in a window? 1 Answer

how to put the GUI into a gameobject so that when clicked, The GUI pops up, and heres a sample multiple Choice question using GUI, i need the code for it. 1 Answer

Is there any way to change GUI btn font? 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