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
-1
Question by SystemIsGod · Jul 27, 2012 at 05:02 PM · c#nguihealth

Public Health Var C#

I'm new to coding, and trying to build a very very basic health system with NGUI and Unity. For the moment I'm trying just trying to get the Progress Bar to update the current HP I have on the player, which I have as an int. I have 2 separate codes, 1 on the gui, and 1 on the player which I'll add damage to, etc. For the moment I'm just trying to link them and having issues figuring it out.

I know this is very bad, but I'm trying to learn, not copy/paste. So far I have this.

PlayerHealth (attacked to player)

 using UnityEngine;
 using System.Collections;
 
 public class PlayerHealth : MonoBehaviour {
  
  private int maxHealth = 300;
  public float Health;
 
  // Use this for initialization
  void Start () {
  Health = maxHealth;
  }
  
  // Update is called once per frame
  void Update () {
  
  }
 }

gui code

 using UnityEngine;
 using System.Collections;
 
 public class VitalBarBasic : MonoBehaviour {
  private UISlider _slider;
  private float _maxWidth;
  private float _health;
  
  void Awake() {
  _slider = GetComponent<UISlider>();
  _maxWidth = _slider.foreground.localScale.x;
  }
  
  void Start() {
  
     _slider = GetComponent<UISlider>();
     _health = Player.GetComponet<Health>();
  
  }
  
  public void Update() {
  _slider.sliderValue = _health/30;
  }
  
  public void UpdateDisplay( float x ) {
  _slider.sliderValue = x;
  }
 
 }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MiniDemonic · Sep 25, 2012 at 06:23 AM

You can use the SendMessage function, look it up on the unity documentation page. http://docs.unity3d.com/Documentation/ScriptReference/GameObject.SendMessage.html

Add a GameObject variable in the GUI script and put your player GameObject in it, create a function in the player script let's call it AlterHealth.

Here is a really simple code for it.

 void AlterHealth(int newHealth){
      health = newHealth;
 }

In the GUI script use this code.

 playerGO.SendMessage("AlterHealth", value);

This will send a message to the player GameObject and execute the function AlterHealth and change the players health to whatever integer value contains.

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

6 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Changing Mesh of an object depending on Health Value. C# 2 Answers

Distribute terrain in zones 3 Answers

Problem with circular health bars 0 Answers

NGUI- Adding children under UIDraggable panel at runtime 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