The question is answered, right answer was accepted
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
}
}
Answer by davidcox70 · Apr 02, 2018 at 11:19 PM
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
I changed it to Invoke, but it doesn't seem to be working. The first function runs, but the Invoke part just doesn't seem to work. This is what I have: ` public void OnQuestionClick(){
helptext.SetActive (true);
Invoke ("removetext", 3);
Debug.Log ("workinginvoke"); //this is shown
}
public void removetext ()
{
helptext.SetActive (false);
Debug.Log ("workingremovetext"); //this does not show up
}
The Debug.Log ("workinginvoke") is shown in the console, but the "workingremovetext" is not. So is the invoke function just not running?
Hmmm. Should work. Just tried this and it works.
Button myButton;
GameObject helpText;
// Use this for initialization
void Start () {
myButton = GameObject.Find ("Button").GetComponent<Button>();
myButton.onClick.AddListener (buttonClick);
helpText = GameObject.Find ("helpText");
helpText.SetActive (false);
}
private void buttonClick(){
helpText.SetActive (true);
Invoke ("removeText", 3);
}
private void removeText(){
helpText.SetActive (false);
}
Hmm, for some reason it's still not working for me when I'm using the exact same script. How did you implement the script? Did you just drag and drop the script file onto the button gameobject? Currently, I'm creating a new gameobject in my hierarchy with the script attached and then dragging that into the OnClick part of the button and choosing the function.
Answer by m1le · Apr 03, 2018 at 10:37 AM
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);
Hello, would this work with a button or would it appear on the home screen and then disappear afterward? I want to create a button for the "helptext" just in case someone doesn't see some sort of text that is instantiated and destroyed on the home screen so they will know what to do.
Hello. GameObject.Destroy(instance,duration); destroy the object after duration time. So it's not a good use if you want the player to close it himself. But you can instantiate it as i says, and call a function destroy when the player click on it... Like this exemple https://docs.unity3d.com/ScriptReference/UI.Button-onClick.html public class ClickExample : $$anonymous$$onoBehaviour { public Button yourButton;
void Start()
{
Button btn = yourButton.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
}
void TaskOnClick()
{
Destroy(gameObject);
}
}