How do I make a gameobject appear and then disappear after some time?

I’m using a button to make some text appear, but then I want it to disappear after 3 seconds. I’m trying to use a Coroutine to do this, but it’s not working. The text appears, but it never goes away.

This is the code for the button:

public GameObject helptext;

public void OnQuestionClick(){

	helptext.SetActive (true);

}

and then this is the code that doesn’t work:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HelpQuestionScript : MonoBehaviour {



	public GameObject helptext;

	void Update(){
		RemoveHelpText ();
	}
	void RemoveHelpText ()
	{
		if (helptext.activeInHierarchy) {
	
			StartCoroutine ("questionfalse");
		}
		else
		{
			return;
		}
	}
		
	IEnumerator questionfalse()
	{
		yield return new WaitForSeconds (3.0f);
		helptext.SetActive (false);
		Debug.Log ("working");  //this never shows up in console
	}

}

You have RemoveHelpText() in your update area - so it is going to fire again and again every frame. Perhaps this is causing the issue.

Another way - Use Invoke.

Just after your “helptext.SetActive(true)” add;
Invoke(“removetext”,3); // this will call the function “removetext” after 3 seconds.

Then your removetext function can just have;
helptext.SetActive(false);

DC

Hello.
A simple way is to put your object in Resources folder, then.
Create your object at runtime :
GameObject instance = Instantiate(Resources.Load(“helptext”, typeof(GameObject))) as GameObject;
And destroy it :
GameObject.Destroy(instance , duration);