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 roegel · Jan 29, 2021 at 05:21 PM · gameobjectcanvaslabel

how can I add a 2D label to a 3D GameObject?

Hello, I have a number of GameObjects in an array, in varying number, and I want to add a UI label at some position over them. I want to do this by script, because my objects may change. I have tried the following, with a Canvas and attaching the script to the main camera, and I see the sphere, but no label is shown. Am I missing something obvious?

Thanks.

 using UnityEngine;
 using UnityEngine.UI;
 
 public class Bug : MonoBehaviour
 {
   Canvas canv;
   GameObject[] obj=new GameObject[100];
   Text objlabel;
 
   void Start()
     {
       canv = GameObject.Find("Canvas").GetComponent<Canvas>();
       Camera.main.transform.position = new Vector3(5, 5, -10);
       obj[0] = GameObject.CreatePrimitive(PrimitiveType.Sphere);
       obj[0].transform.position = new Vector3(-7.5f,6.0f,1.4f);
       obj[0].SetActive(false); // hide the sphere
       
       GameObject golab=new GameObject("Lab1");
       objlabel=golab.AddComponent<Text>();
       objlabel.text="XXXXX";
       objlabel.transform.SetParent(canv.transform);
 
       Vector3 namePos=Camera.main.WorldToScreenPoint(obj[0].transform.position);
       objlabel.transform.position=namePos;
     }
 
   void Update()
     {}
 }
 
Comment
Add comment · Show 1
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 roegel · Jan 29, 2021 at 05:22 PM 0
Share

An auxiliary question is how I can set the canvas to adjust to the main camera view?

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by CmdrZin · Jan 29, 2021 at 07:46 PM

Here's some code that I use for that.

         // Add namePlate here after instaniation.
         namePlate = new GameObject("NamePlate");
         namePlate.AddComponent<TextMesh>();            // To display name.
         // Adjust position and settings.
         TextMesh textMesh = namePlate.GetComponent<TextMesh>();
         if (textMesh != null)
         {
             Debug.Log("Adding Nameplate");
             textMesh.transform.position = player.transform.position + new Vector3(0, 1.3f, 0);  // elevate y
             textMesh.transform.rotation = player.transform.rotation * new Quaternion(0, 180.0f, 0, 0);  // turn it around
             textMesh.characterSize = 0.2f;
             textMesh.fontSize = 10;
             textMesh.alignment = TextAlignment.Center;
             textMesh.anchor = TextAnchor.MiddleCenter;
             textMesh.color = Color.white;
             textMesh.text = id;     // Set the name text to the network id string.
         }
         // Now set the new Player object as its parent.
         namePlate.transform.parent = player.transform;
 
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 roegel · Jan 30, 2021 at 03:47 PM 0
Share

Thanks. This works, but the problem is that the size of the text depends on the distance of the GameObject. I really would like to add some text in 2D at a constant distance in front of the camera and at some position corresponding to a 3D object. This is why I had initially used Camera.main.WorldToScreenPoint and a Canvas.

avatar image
0

Answer by roegel · Jan 30, 2021 at 12:02 PM

Thanks, here is my improved version, without a Canvas:

 public class Bug2 : MonoBehaviour
 {
   GameObject[] obj=new GameObject[100];
   TextMesh objlabel;
 
   void Start()
     {
       Camera.main.transform.position = new Vector3(5, 5, -10);
       obj[0] = GameObject.CreatePrimitive(PrimitiveType.Sphere);
       obj[0].transform.position = new Vector3(-7.5f,6.0f,1.4f);
       obj[0].SetActive(true); // if false, hide the sphere (and the label)
       
       GameObject golab=new GameObject("Lab1");
       objlabel=golab.AddComponent<TextMesh>();
       objlabel.text="Sphere";
       objlabel.alignment = TextAlignment.Center;
       objlabel.anchor = TextAnchor.MiddleCenter;
       objlabel.color = Color.red;
 
       objlabel.transform.parent=obj[0].transform; 
       objlabel.transform.position=obj[0].transform.position+new Vector3(0,2,0);
     }
 
   void Update()
     {}
 }
 
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

176 People are following this question.

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

Related Questions

How to find UI in code c# (button, Panel etc) 2 Answers

Changing alpha value of a canvas from a different game object 1 Answer

Masking GameObject in Canvas 1 Answer

Game Object not moving in Canvas 0 Answers

Resize every GameObject after screen resolution? 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