Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
This question was closed May 22, 2016 at 09:09 AM by vinit_ingle for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by vinit_ingle · Dec 23, 2014 at 10:56 AM · uiprefabtextprefabs

Display text above prefabs in Unity 4.6

Hi, 'm new to unity and stuck on a point. I want to display a timer as text above prefabs that are generated after random amount of time. I have made a Text object which is inside a Canvas. The problem is that text is displayed over the prefab which is on screen, but when another prefab is generated the text on the current prefab vanishes and moves on top of the new prefab. Following is the code. Thanks in advance people.

public class DestroyBomb : MonoBehaviour {

 Text cntDownTimer;
 private bool isGrounded = false;
 public float timer = 5;

 void Start()
 {
     cntDownTimer = GameObject.Find ("Text").GetComponent<Text>();
 }

 void Update () 
 {
     if (isGrounded)
     {
         timer -= Time.deltaTime;
     
         cntDownTimer.rectTransform.position = new Vector3(transform.position.x + 2.15f, transform.position.y + 0.5f, 0f);        
         cntDownTimer.text = timer.ToString("0");
     }
 }

 void OnCollisionEnter2D(Collision2D col)
 {
     if(col.collider.gameObject.tag == "Ground")
         isGrounded = true;    
 }

}

Comment
Add comment · Show 2
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 gjf · Dec 22, 2014 at 04:11 PM 1
Share

it's probably because you're searching for any object called "Text" and it will return the first one it finds.

if the Text component is on the same object, then try

 gameObject.GetComponent<Text>();

ins$$anonymous$$d... it wouldn't do any harm to add in some null checks too.

avatar image vinit_ingle · Dec 23, 2014 at 08:01 AM 1
Share

hey thnx man....because of ur suggestion i got an idea....earlier text was a seperate object....now i made the text object as a child of my prefab....and now each prefab is getting its own overhead text....thnx a lot....the only thing i changed in the above code was the following....

void Start() {
cntDownTimer = gameObject.GetComponentInChildren (); }

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

What's your equivalent of old GUIStyle ? 0 Answers

UI Text.color not assigning? 2 Answers

Runtime Changes to UI Object From Prefab Unexpectedly Changing All Prefab Objects 1 Answer

Passing through a GameObject/Function to a button's OnClick 1 Answer

Trouble instantiating ui text 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