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 mrapple · May 31, 2012 at 03:27 AM · floatnguihealth

Trying to get 2 floats synced up.

Hey guys, using NGUI to make a Health bar, problem is that the progress bars float has a value of 0 to 1, but my Health is 0 - 100, I was wondering how I would be able to hook it up so that the health and the NGUI bar is the same value and will increase and decrease respectfully.

Here's what I have so far, I just don't know how to hook it up.

 using UnityEngine;
 using System.Collections;
 
 public class HealthBarLink : MonoBehaviour {
     
     private UISlider _slider;
     public GameObject Player;
     
     void Update()
     {
         _slider = GetComponent<UISlider>();
         Health = Player.GetComponent<Health>();
         
         Health.curHealth == _slider.rawValue;
     }
         
         
 
 }

The rawValue part is where I'm having trouble.

Comment
Add comment · 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 hijinxbassist · May 31, 2012 at 05:11 AM 0
Share

divide ur health by 100

avatar image mrapple · May 31, 2012 at 07:10 AM 0
Share

I knew it'd have to be something like, not sure how to properly implement it in code though. :/

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by hijinxbassist · May 31, 2012 at 08:12 AM

 void Update()
 {
    _slider = GetComponent<UISlider>();
    Health = Player.GetComponent<Health>();

    _slider.rawValue = Health.curHealth/100;
 }

I think this is what you want. However you dont need to do it in update, just change the rawValue to curHealth/100 whenever the health decreases. This way you do not need to check/change the value every frame. Heres an example of how to update the rawValue when your player takes damage.

 //Health.cs
 void Damage(float damage)
 {
     curHealth-=damage;
     _slider = GetComponent<UISlider>();
     _slider.rawValue=curHealth/100;
 }
Comment
Add comment · Show 1 · 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 mrapple · May 31, 2012 at 08:30 AM 0
Share

Cheers mate, appreciate the help.

avatar image
1
Wiki

Answer by jorgenpt · May 31, 2012 at 08:54 AM

You probably want to do the following:

 // Cache these values on startup
 void OnEnable ()
 {
     _slider = GetComponent<UISlider>();
     Health = Player.GetComponent<Health>();
 }

 // Make UISlider match health
 void Update()
 {
     _slider.sliderValue = Health.curHealth/Health.maxHealth;
 }

Assuming you have a Health.maxHealth. :-)

I hope this helps!

Comment
Add comment · Show 1 · 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 jorgenpt · May 31, 2012 at 09:12 PM 0
Share

Yeah, like hijinxbassist says - you should probably only update sliderValue in your "Damage" code, as it'll be faster. (and it'll be the only place the value of the slider changes)

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

Slider calculations go wrong 1 Answer

Calling a c# script from js [for NGUI] 1 Answer

Public Health Var C# 1 Answer

Float not resetting when I instantiate new player prefab 1 Answer

Health script 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