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 furibaito · Dec 26, 2014 at 05:17 AM · uisizesliderhp

Dynamic slider size with the new UI

Hello, I'm quite new at Unity and programming and now I'm having problem at displaying a dynamic size HP Bar. What I want to achieve is a HP Bar that the length scales with the maximum HP variable (By script). These pictures should explain these more..

alt text alt text

These can be achieved by manually modifying the "Right" value in Rect Transform inspector. But through script I have no idea. I've tried modifying localscale values but come with no luck. Thank you.

aa.jpg (7.5 kB)
bb.jpg (9.5 kB)
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
0
Best Answer

Answer by furibaito · Dec 27, 2014 at 06:03 AM

I've solved my problem, here is the solution if anyone else need it.

 public float PlayerMaxHP = 50;
 public GameObject HPSlider;
 
 void Start () 
 {
     RectTransform HPSliderRect= HPSlider.GetComponent<RectTransform>();
     HPSliderRect.sizeDelta = new Vector2(PlayerMaxHP, HPSliderRect.sizeDelta.y);
 }

The value that I need to change is .sizeDelta on the RectTransform component of the HPSlider gameobject.

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 Mmmpies · Dec 26, 2014 at 09:06 AM

If I'm right the you want the HP slider to be the same size no matter how much HP you have so

HP = 200 HP bar size = ############## HP = 100 HP bar size = ##############

is that right?

If so this is what I do with my player health script:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class PlayerHealth : MonoBehaviour {
 
     public static float maxHealth = 200;
     public static float curHealth = 100;
     public float restoreHealth = 0.5f;    // how much to restore health per second
     public GameObject HUDUICanvas;
     public GameObject HealthBar;
     public Slider HSlider;
 
     private string _loadedLevel;
 
     // Use this for initialization
     void Start () {
         HUDUICanvas = GameObject.FindGameObjectWithTag("UI");
         HealthBar = GameObject.FindGameObjectWithTag("HealthBar");
 
         HSlider = HealthBar.GetComponentInChildren<Slider>();
     }
     
     // Update is called once per frame
     void Update () {
         if (curHealth < maxHealth)
         {
             AdjustCurrentHealth(restoreHealth * Time.deltaTime);
         }
     }
 
 
 
     public void AdjustCurrentHealth(float adj)
     {
         curHealth += adj;
 
         if(curHealth < 0)
             curHealth=0;
 
         if(curHealth > maxHealth)
             curHealth = maxHealth;
 
         if(maxHealth < 1)
             maxHealth = 1;
 
         HSlider.value = (curHealth / maxHealth);
 
         if(curHealth == 0)
         {
             Debug.Log(" I'm dead.");
         }
     }
 }


The maxHealth and CurHealth can be any number, I then find the

 <slider>

component and store in HSlider.

When the health is adjusted I set the HSlider.value = (curHeath / maxHealth).

All it does is make it display the curHealth as a % of the maxHealth so the maximum size of the slider is 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 furibaito · Dec 27, 2014 at 04:46 AM 0
Share

Sorry, but what I need is to make the HP slider to be different size when the value $$anonymous$$axHP is different. Thanks though for the answer

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Unity 4.6: GetComponent().onClick... how do you add an event to button click? 6 Answers

How to create a gradient progress bar 3 Answers

Get UI Slider Value 3 Answers

Trying to move slider based off percentage of screen 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