• 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
Question by David 18 · Dec 09, 2010 at 08:44 PM · guiupdate

Health won't update in GUI

Basically, I'm trying to make it so that this code updates my GUI to keep track of how much health the player and enemy has. Only problem is that it won't update the GUI, it just stays at 2. The print quotes output the correct health in the console, but not in the GUI. I've only attached this script to my CannonBall and my GUI Text file. Any help would be much appreciated.

var hpPlayer : int = 2; var hpEnemy : int = 2; var playerDead : boolean = false; var enemyDead : boolean = false; var x = 1;

function OnTriggerEnter(hit : Collider) { if (collider.gameObject.tag == "CannonBall") { Destroy(collider.gameObject); } }

function OnCollisionEnter(collision : Collision){ if(collision.gameObject.tag == "Player"){ hpPlayer--; print("hit player " + hpPlayer); if (hpPlayer<=0) playerDead = true; }

   if(collision.gameObject.tag == "Enemy"){
    hpEnemy--;
    print("hit enemy " + hpEnemy);
    if (hpEnemy&lt;=0) enemyDead = true;

} }

function OnGUI () { hpP = hpEnemy + ""; GUI.Label (Rect (10, 10, 100, 20), "HP Player: " + (hpPlayer).ToString()); GUI.Label (Rect (10, 30, 100, 20), "HP Enemy: "(hpEnemy).ToString()); }

Comment

People who like this

0 Show 2
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 oliver-jones · Dec 09, 2010 at 08:57 PM 0
Share

Add a print statement in an Update Function and see if 'hp' is actually moving.

avatar image David 18 · Dec 09, 2010 at 09:21 PM 0
Share

I can already tell that hp isn't moving by looking at the inspector. I just have no idea because my other print function is showing that it went down by one, when the inspector isn't.

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by coastwise · Dec 14, 2010 at 03:53 PM

Your trouble lies in the fact that this script is instantiated once for every GameObject it is attached to. The instance that is attached to your GUI has its own copy of the hpPlayer and hpEnemy variables, and since the GUI is never colliding with the player, these copies that are being displayed are never being modified.

Additionally, I'm assuming your using Instantiate on each of your cannon balls. Each of these will also have their own copy and you have the same problem.

The quick fix here is to declare those shared variables as static, that way there is only one copy that is shared between every instance of the script. Check out this explanation from the Script Reference.

Comment

People who like this

0 Show 0 · 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

No one has followed this question yet.

Related Questions

Input versus Event 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Setting Scroll View Width GUILayout 1 Answer

Is this a problem of unity 3 or i am doing something wrong. 1 Answer

What's wrong with my function? It keeps telling me 'loseText' is not a member of 'UnityEngine.GUIText'. Please help, please and thank you! 0 Answers


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