Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Apr 03, 2018 at 11:43 PM by S_jay1 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by S_jay1 · Apr 02, 2018 at 08:33 PM · gamecoroutineienumerator

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
     }
 
 }
 

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image S_jay1 · Apr 03, 2018 at 07:05 PM 1
Share

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?

avatar image davidcox70 S_jay1 · Apr 03, 2018 at 08:31 PM 1
Share

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);
     }
avatar image S_jay1 davidcox70 · Apr 03, 2018 at 09:41 PM 0
Share

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.

Show more comments
avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image S_jay1 · Apr 03, 2018 at 07:08 PM 0
Share

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.

avatar image m1le S_jay1 · Apr 04, 2018 at 11:34 AM 0
Share

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

}

Follow this Question

Answers Answers and Comments

168 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What is Coroutine, Yiel and IENumerator? 1 Answer

"Can't add script behaviour AICharacterControl. The script needs to derive from MonoBehaviour!" ? 2 Answers

Weird behaviour with mathf.lerp 0 Answers

How to get the Progress from a IEnumerator 0 Answers

When to use IEnumerator ? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges