Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 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 /
avatar image
0
Question by spires_ghost · Apr 01, 2017 at 08:08 AM · instantiatetextcanvasprefabsgetcomponentinchildren

Text Not Being Displayed above Instantiated Prefabs

I have a timer being displayed above my enemy sprites that count down consistently. The trouble is it doesn't work with prefabs that are instantiated in runtime. If I drag the prefab into the scene before running, the timer will show up just fine but those spawned in by my spawner script display nothing.

This is on the enemy character object:

public float lifetime; public Text textField;
public string timeLeft; enemyTextScript myScript; public GameObject myTextMesh;

 // Use this for initialization
 void Start () {

     myScript = gameObject.GetComponentInChildren(typeof(enemyTextScript)) as enemyTextScript;

 }

 // Update is called once per frame
 void Update () {

     lifetime -= Time.deltaTime;
     timeLeft = lifetime.ToString ("F2");
     textField = GetComponent<Text>();
     timeLeft = lifetime.ToString ("F2");

     myScript.report(timeLeft);


     if (lifetime <= 0) {
         Destroy (gameObject);
         lifeTime.instance.lifetime += 2f; 

     }
 }


 void OnCollisionEnter2D(Collision2D col){
     if (col.gameObject.tag == "pAttack") {
         if (col.gameObject.name == "slash(Clone)") {
             lifetime -= 3f;

         }
         if (col.gameObject.name == "shuriken(Clone)") {
             lifetime -= 5f;
         }

     }
 }

 void reportTime(){
     timeLeft = lifetime.ToString ("F2");
 }

And this is on the text object, a child of the enemy (and part of a canvas)

public Text textField; public string timeL;

 // Use this for initialization
 void Start () {

 
     textField = GetComponent<Text>();


 }

 // Update is called once per frame
 void Update () {

     textField = GetComponent<Text>();

     textField.text = ("" + timeL);
 }
     

 public void report(string time){
     textField.text = ("" + time);
 }

Thank you for any help you can offer, it's much appreciated.

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 spires_ghost · Mar 29, 2017 at 06:24 PM 0
Share

Update: I think it has something to do with the instantiated prefabs not being setActive. I tried adding a text mesh ins$$anonymous$$d of a canvas with text as I saw that canvases are challenging to alter the active status. I changed every instance of Text in the second script to Text$$anonymous$$esh but its having exactly the same effect.

0 Replies

· Add your reply
  • Sort: 

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

Text very big for one frame 0 Answers

How do you Instantiate Text as Child of Canvas c# 1 Answer

cachedTextGenerator not working with instantiated canvas 1 Answer

Creating Buttons with a C# Script 1 Answer

Instantiate prefab text into Canvas 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