Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Spinzaku · Aug 27, 2015 at 12:20 PM · variablesguitextdisplay

Is it possible to display a variable relative to an object at runtime e.g. health in numbers above an enemy?

What I'm asking was really simple for me to do in other game engines but I haven't really gotten around to get it to work in Unity. I'm trying to get all the enemies in my game to display the value of their health variable on top of them at runtime. There can be multiple instances of the same enemy in the game at the same time and most of these objects get instantiated during runtime.

The code I that I have for the enemy so far:

 using UnityEngine;
 using System.Collections;
 
 public class CreatureScript : MonoBehaviour
 {
     public int dir = 0;
     public int changetime = -1;
     private int timer = 0;
     public int mspeed = 2;
     public int side = 0;
     public int health = 2;
     public int attack = 1;
     CreatureScript creatureScript;
     BulletScript bulletScript;
     void OnTriggerEnter2D(Collider2D other)
     {
         if(other.tag == "wall") //Collision with wall
         {
             mspeed = mspeed * -1;
         }
         if(other.tag == "creature") //Collision with creature
         {
             mspeed = mspeed * -1;
             CreatureScript creatureScript = other.GetComponent<CreatureScript>();
             if(side != creatureScript.side)
             {
                 health -= creatureScript.attack;
                 if(health < 1)
                 {
                     Destroy(gameObject);
                 }
             }
         }
     }
     
 
     void FixedUpdate ()
     {    
         if(timer == changetime) //set random direction
         {
             timer = 0;
             dir = Random.Range(0,4);
         }
         timer = timer + 1;
         //Change direction
         if(dir == 0)
         transform.Translate (new Vector3(mspeed*Time.deltaTime,0*Time.deltaTime,0));
         if(dir == 1)
         transform.Translate (new Vector3(-mspeed*Time.deltaTime,0*Time.deltaTime,0));
         if(dir == 2)
         transform.Translate (new Vector3(0*Time.deltaTime,mspeed*Time.deltaTime,0));
         if(dir == 3)
         transform.Translate (new Vector3(0*Time.deltaTime,-mspeed*Time.deltaTime,0));
 
     }
 }

I already tried playing around with the new UI System but haven't really found an option that makes this work yet.

Thanks for your answers in advance.

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
Best Answer

Answer by cjdev · Aug 27, 2015 at 07:46 PM

Yes you can position UI elements on screen based on your GameObject's world coordinates by transforming them into screen coordinates. Here's an example, although it's done entirely in script so I would recommend making a text prefab and loading it from the Resources folder unless you're comfortable with C#:

 //using UnityEngine.UI;
 
 void Start()
 {
         //Make sure there's a Canvas UI object in your scene named 'Canvas'
         //Note: it is much easier to make a prefab and load it from resources than to do this
         // vvvvvvvvv
         canv = GameObject.Find("Canvas").GetComponent<Canvas>();
         canvTransform = canv.GetComponent<RectTransform>();
         canv.renderMode = RenderMode.ScreenSpaceOverlay;
         GameObject txtObj = new GameObject("Text");
         txt = txtObj.AddComponent<Text>();
         txtObj.transform.SetParent(canv.transform, false);
         txt.font = Font.CreateDynamicFontFromOSFont("Arial", 16);
         txt.alignment = TextAnchor.UpperCenter;
         // ^^^^^^^^^
         txt.text = health.ToString();
 }
 
 void Update()
 {
         Vector2 screenPoint = RectTransformUtility.WorldToScreenPoint(Camera.main, transform.position);
         txt.rectTransform.anchoredPosition = screenPoint - canvTransform.sizeDelta / 2f;
         txt.text = health.ToString();
 }



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 Spinzaku · Aug 27, 2015 at 10:31 PM 0
Share

Thanks a lot for the answer. I had some trouble implementing the code at first but in the end it all worked now. For other people who stumble upon this question make sure to add these 3 lines of code, to the initialisation(I'm not sure what exactly you call this part yet) at the start of the class: Canvas canv; RectTransform canvTransform; Text txt; Also make sure that you are using the UnityEngine.UI, as cjdev stated in his script.

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

How to display EULA from Doc in Unity? 0 Answers

Change the game window built 1 Answer

When I try to increment a variable by 1, why does it add 10? 1 Answer

Reference other scrips in prefab 1 Answer

Score & High Score logic doesn't work 2 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