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. Dismiss Notice

Instantiate Text then delete it

Discussion in 'UGUI & TextMesh Pro' started by HappyLDE, Aug 23, 2014.

  1. HappyLDE

    HappyLDE

    Joined:
    Apr 16, 2014
    Posts:
    56
    So I instantiate a text prefab like so:
    Code (CSharp):
    1. theText = (Text)Instantiate(GuiText);
    2.  
    3. Destroy(theText);
    But when i destroy it, the game object is not destroyed, only it's Text Component.


    What's wrong?
     
    Last edited: Aug 23, 2014
  2. HappyLDE

    HappyLDE

    Joined:
    Apr 16, 2014
    Posts:
    56
    Fixed it by instantiating a GameObject and then taking it's Text component to work on it (like assigning it's parent to Canvas).

    Code (CSharp):
    1. guiTextObj = (GameObject)Instantiate(GuiText);
    2. textComponent = guiTextObj.GetComponent<Text>();
     
    Last edited: Aug 23, 2014
  3. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    Try destroy theText.gameObject? Or don't cast?
     
    HappyLDE and Tim-C like this.
  4. HappyLDE

    HappyLDE

    Joined:
    Apr 16, 2014
    Posts:
    56
    Oh your method is better!

    Thanks :D