0

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

6
  • possible duplicate of Randomly appearing and disappearing objects in Unity Commented Sep 1, 2015 at 13:01
  • Follow the duplicated answer link, but remove the code that toggles the view Commented Sep 1, 2015 at 13:02
  • can I attach my text as game object
    – Mina Fawzy
    Commented Sep 1, 2015 at 13:08
  • 1
    If the problem is with showing the object, a checklist you could walk through: 1) Is the component active? 2) Is the gameobject active? 3) Is the gameobject's hierarchy (parent) active? 4) Does the gameobject have a renderer (that's active)? 5) Is the text field non-empty?
    – Aidiakapi
    Commented Sep 1, 2015 at 13:13
  • @minafawzy if you are using the new unity gui all elements in the view need to be in GameObject (they are Components, you can't actually create Components outside GameObjects) You should edit your question and specify how the scene in Unity is created, for example, what components do the GameObject named "3dtext" contains? Why are you pointing to the TextMesh class documentation but in your example you use the Text class? Commented Sep 1, 2015 at 13:51

3 Answers 3

3

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);

     }

   }
}
0
1

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.

3
  • 1
    Make sure that the script with the Coroutine isn't on the same gameObject or on a child gameObject, since coroutines don't continue running when the object is deactivated.
    – Aidiakapi
    Commented Sep 1, 2015 at 13:18
  • thank you for answer , I find enable method GetComponent(TextMesh).text.renderer.enabled = true; could you explain this code with full example
    – Mina Fawzy
    Commented Sep 1, 2015 at 13:20
  • @Aidiakapi I did not actually know this! I'll integrate that into my answer
    – David
    Commented Sep 1, 2015 at 13:21
1

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);
}
1
  • @minafawzy It won't matter. Compared to operations like rendering, updating the scene graph, de/reactivating objects, a simple schedule like this won't matter. Some versions of Invoke depend on a relatively slow reflection (though it might be cached or precalculated in Unity's pipeline), while a coroutine creates a collectible state machine. But in general, unless you're creating a coroutine or scheduling an invoke hundreds of times per second (in which case there are probably better solutions), it won't matter one bit.
    – Aidiakapi
    Commented Sep 2, 2015 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.