Mina Fawzy
Mina Fawzy

Reputation: 21452

Unity - how can I show / hide 3D Text

how can I display 3D text after certain time then hide it after certain time

My tries

public Text text_tap;

GameObject.Find("3dtext").active = true; // first try but it dosnt work

if (Time.time > 5) {

           // second try but it I cant attach my 3d text to my script
            text_tap.gameObject.SetActive(true);

        }

I cant find any thing in 3D documentation

Upvotes: 0

Views: 10741

Answers (3)

Justin Markwell
Justin Markwell

Reputation: 341

you can also use Invoke() to achieve. as explained above note that the text would have to be set by other means than Find cause if it is not active it will not find it.

void Start() //or any event
{
   Invoke("ShowTextTap", 5f);//invoke after 5 seconds
}
void ShowTextTap()
{
    text_tap.gameObject.SetActive(true);
    //then remove it
    Invoke("DisableTextTap", 5f);
}
void DisableTextTap()
{
    text_tap.gameObject.SetActive(false);
}

Upvotes: 1

Ricardo Amores
Ricardo Amores

Reputation: 4687

I don't know the exactly problem, but there you have some hints:

If you search in the scene for a GameObject that is deactivated it won't find it. The Gameobject MUST be active for the GameObject.Find() function to work. The easiest thing you can do is to keep the GameObject activated, and if the initial state is for it to stay hidden just hide it in the Awake().

Secondly, seems that you are trying to access a TextMesh object but you reference in your code a Text object. If you find a GameObject and request a Component that the GO does not contains, it returns null.

Finally The api to Activate/Deactivate a GameObject (GO) is

myGameobject.SetActive(true)

The one you are using (myGameobject.active = true) is deprecated

Try this example, it should work:

public YourMonoBehaviour : MonoBehaviour
{
  public TextMesh text_tap;

  float awakeTime;

  void Awake()
  {
    // Remember to activate the GO 3dtext in the scene! 
    text_tap = GameObject.Find("3dtext").GetComponent<TextMesh>():

    awakeTime = Time.time
  }


   void Update()
   {
     if ((Time.time - awakeTime) > 5) 
     {

       // second try but it I cant attach my 3d text to my script
       text_tap.gameObject.SetActive(true);

     }

   }
}

Upvotes: 3

David
David

Reputation: 10708

If you need to "do something after a delay" you're talking about Coroutines.

Checking Time.time will only check if the game has been running for x time, and using Thread.Sleep in Unity will cause it to delay since you're causing an Update or similar to lock and not return.

Instead, use

yield return WaitForSeconds(5);
text_tap.gameObject.SetActive(false);

As another warning, this code assumes that the target object is not the same gameObject as the one hosting this script, since coroutines do not execute on inactive objects. Similarly, disabling an ancestor (via the scene hierarchy, or transofrm.parent) of a gameObject disables the gameObject itself.

If this is the case, get the component that renders 3d text and disable it instead of the whole gameObject via the enabled field.

Upvotes: 1

Related Questions