Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 BokuDev · Jul 07, 2015 at 03:58 PM · camerauigui

Changing old GUI health code to new Unity UI

Iv been using a GUI script for health for awhile now but I want to convert to the new Unity UI and I can't figure out how. I keep getting errors when I switch things to have the new UI labels like "Text" etc. Can anyone help me out? I want to be able to use the new Unity UI so I can have my GUI be in world space canvases. Here is my code its also C#

 using UnityEngine;
 using System.Collections;
  
 //ATTACH TO MAIN CAMERA, shows your health and coins
 public class GUIManager : MonoBehaviour
 {      
         public GUISkin guiSkin;                                 //assign the skin for GUI display
         [HideInInspector]
         public int coinsCollected;
  
         private int coinsInLevel;
         private Health health;
        
         //setup, get how many coins are in this level
         void Start()
         {
                 coinsInLevel = GameObject.FindGameObjectsWithTag("Coin").Length;               
                 health = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerHealth>();
         }
        
         //show current health and how many coins you've collected
         void OnGUI()
         {
                 GUI.skin = guiSkin;
                 GUILayout.Space(5f);
                
                 if(health)
                         GUILayout.Label ("Health: " + health.currentHealth);
                 if(coinsInLevel > 0)
                         GUILayout.Label ("Cubes: " + coinsCollected + " / " + coinsInLevel);
         }
 }
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
1

Answer by DoTA_KAMIKADzE · Jul 08, 2015 at 02:01 AM

Unity UI doesn't exactly work like that, that's a whole different story, you're no longer tied to OnGUI, you're now tied to objects, because new UI's are components.

You can clone behaviour of your "style", but I would strongly recommend to not do it that way, let me try to explain why, here are the bare bones of how your clone would ~look like:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 //ATTACH TO MAIN CAMERA, shows your health and coins
 public class GUIManager : MonoBehaviour
 {
     [HideInInspector]
     public int coinsCollected;
 
     private int coinsInLevel;
     private Health health;
     private Text yourHealthText;
     private Text yourCubesText;
     //setup, get how many coins are in this level
     void Start()
     {
         coinsInLevel = GameObject.FindGameObjectsWithTag("Coin").Length;
         health = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerHealth>();
         yourHealthText = GameObject.Find("yourHealthTextObject").GetComponent<Text>();//or public/[SerializeField] and assign it through inspector instead or GameObject.FindObjectsOfType<Text> and loop through them to find or...
         yourCubesText = GameObject.Find("yourCubesTextObject").GetComponent<Text>();//or public/[SerializeField] and assign it through inspector instead or GameObject.FindObjectsOfType<Text> and loop through them to find or...
     }
 
     //show current health and how many coins you've collected
     void Update()
     {
         if (health) yourHealthText.text = "Health: " + health.currentHealth;
         if (coinsInLevel > 0) yourCubesText.text = "Cubes: " + coinsCollected + " / " + coinsInLevel;
     }
 }

First of all - performance, unless your health and coins incr/decr faster than frames come out then you'll have performance loss. As instead you could create some function that will repeat itself in X time and update text or create 2 separate functions for incr/decr health and coins that will set respective values + update the text, and here we come to a second point - why would you get objects, incr/decr their values and display text not in the respective object in the first place, or in the function where you incr/decr respective values? I guess that's enough of explanations and you already see where I'm going.

So what should you do? If you don't mind all of the above^ then go for that example that I've shown and correct it for your taste (obviously referencing to your actual texts, etc.), but if you want to better understand and get in touch with new UI, I'd suggest you to check out Unity's UI tutorials and Unity's UI examples. Also there are plenty of 3rd party UI health bar examples and tutorials with different concepts on the web, you can use your favourite search engine to find them out.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I calculate whether an UI element is inside my camera view? 0 Answers

Changing where another Camera renders 0 Answers

Unity 3D UI and Standard Transparent material 0 Answers

3rd party UI - World Canvas or Screen Space Camera? 0 Answers

How can I convert a world space point to screen, with canvas scaling? 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