Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Floating Text Attached to Object

Discussion in 'Scripting' started by LegendXV, Jun 2, 2015.

  1. LegendXV

    LegendXV

    Joined:
    May 31, 2015
    Posts:
    30
    I'm trying to attach floating text to a sphere object (making bubbles with numbers over them). I can't attach a textmesh to it because it conflicts with the mesh of the sphere. What do I do?
     
  2. Crayz

    Crayz

    Joined:
    Mar 17, 2014
    Posts:
    192
    Something I wrote up last night pools a GameObject with UI.Text component attached, modifies its values, parents to a Canvas, and uses Camera.main.WorldToScreenPoint(owner.transform.position) to set its position within Update. Owner being the object the text floats above.

    The text tends to drag a bit when the camera or object moves though, have yet to continue working on this:
    https://dl.dropboxusercontent.com/u/96502721/S/drag.mp4
     
  3. LegendXV

    LegendXV

    Joined:
    May 31, 2015
    Posts:
    30
    I just need something simple, like an NPC name hovering over his or her head. I'm super confused on what to do.
     
  4. Crayz

    Crayz

    Joined:
    Mar 17, 2014
    Posts:
    192
  5. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Kiwasi likes this.
  6. LegendXV

    LegendXV

    Joined:
    May 31, 2015
    Posts:
    30
    Currently I have no code using the textmesh, it's just I tried attaching a TextMesh component to my spherical object and it told me I couldn't.

    Edit: How do I assign a text UI to a prefab through script? I can't drag and drop the reference because it's a prefab. Here's my code:

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5.  
    6. public class Clickable : MonoBehaviour {
    7.  
    8.     private Player mainPlayer;
    9.     private int number;
    10.     private bool clickedOn = false;
    11.     public Text txt;
    12.  
    13.     // Use this for initialization
    14.    
    15.     void Start() {
    16.  
    17.     }
    18.  
    19.     public void initiate(Player p, int numb) {
    20.         mainPlayer = p;
    21.         number = numb;
    22.     }
    23.    
    24.     // Update is called once per frame
    25.     void Update () {
    26.  
    27.     }
    28.  
    29.     public int GetNumber() {
    30.         return number;
    31.     }
    32.  
    33.     public Player GetPlayer() {
    34.         return mainPlayer;
    35.     }
    36.  
    37.     public void OnMouseDown() {
    38.         if (clickedOn) {
    39.             print ("Number: " + GetNumber () + " at X position: " + this.transform.position.x);
    40.             GetPlayer ().AddSelection (GetNumber ());
    41.         } else {
    42.             txt.text = "Console: Hi! My number is " + GetNumber ();
    43.             clickedOn = true;
    44.         }
    45.     }
    46. }
     
    Last edited: Jun 2, 2015
  7. Crayz

    Crayz

    Joined:
    Mar 17, 2014
    Posts:
    192
    This is probably the best way, I'll probably end up re-writing my UI for this

    op, to do this..
    You can create your prefab with a UI.Text component already attached. I recommend creating an empty child object with a Canvas and Text component attached. Make sure the Canvas Render Mode is set to World Space. Afterwards you can reference it easily by doing something of the sort..
    Code (csharp):
    1. txt = gameObject.GetComponentInChildren<Text>();
     
  8. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Have you tried adding an empty child object to the sphere, and then attaching the textmesh to that child? (I haven't used a textmesh, but this is the work-around for the limitation that you can't put two graphical components on a single UI object.)
     
    Kiwasi likes this.
  9. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    World space canvas is your best bet. You can watch me fumble around with attaching them to objects at runtime in this video.