Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

How to make a button appear after a few seconds.

Discussion in 'Scripting' started by CCouto96, May 28, 2018.

  1. CCouto96

    CCouto96

    Joined:
    May 18, 2016
    Posts:
    44
    Hello everyone!

    I am in need of some programming assistance.

    I am trying to create code that will basically tell the GUI button object in my scene to appear on the canvas after a few seconds.
    I created a script that I saw on another forum thread and applied it to my button, but it won't work. It makes the button disappear after a while, which isn't what I want. I tried changing up the code so it will do the opposite but I haven't had much luck.

    I will attach the code to this thread and would appreciate any help I can get. I'm not really a programmer, so please excuse any stupid mistakes.

    Thank you so much!

    My Code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ButtonAppearance : MonoBehaviour {
    6.  
    7.     public GameObject Option_1;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.  
    12.         StartCoroutine(ShowAndHide(Option_1, 2.0f) );
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update () {
    17.      
    18.     }
    19.  
    20.     IEnumerator ShowAndHide(GameObject Option_1, float delay)
    21.     {
    22.         Option_1.SetActive(true);
    23.         yield return new WaitForSeconds(delay);
    24.         Option_1.SetActive(false);
    25.     }
    26. }
    27.  
     
    Last edited: May 28, 2018
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Well, one thing to keep in mind, you have a public variable Option_1 and you're passing that into your coroutine, but since the variable is a class variable, the coroutine already has access to it, so you don't have to pass it in. Now, this isn't the source of your problem.

    You just need this script on some object in your scene that is active and have the button assigned to the Option_1 variable.

    It will then turn on the button and then turn it off after 2 secs.
     
  3. Karrzun

    Karrzun

    Joined:
    Oct 26, 2017
    Posts:
    123
    CCouto96 wants to achieve the opposite, though. So you want to SetActive(false) first, and then after waiting SetActive(true). You should accordingly change the name of the function to "HideAndShow".

    Implementing both, Brathnann's and my additions should leave you with something like this:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ButtonAppearance : MonoBehaviour {
    6.  
    7.     public GameObject Option_1;
    8.  
    9.     // Use this for initialization
    10.     void Start () {
    11.         StartCoroutine(HideAndShow(2.0f) );
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update () {
    16.    
    17.     }
    18.  
    19.     IEnumerator HideAndShow(float delay)
    20.     {
    21.         Option_1.SetActive(false);
    22.         yield return new WaitForSeconds(delay);
    23.         Option_1.SetActive(true);
    24.     }
    25. }
    26.  
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Just to mention, the script could reduce down to only the Start method for simplicity. Also, CCouto96, you should consider making Option_1 private and either have it set in the Editor (using the
    [SerializeField]
    attribute) or using something like
    GetComponent<Button>()
    to find it. :
    Code (CSharp):
    1. public class ButtonAppearance : MonoBehaviour
    2. {
    3.     public GameObject Option_1;
    4.  
    5.     IEnumerator Start()
    6.     {
    7.         Option_1.SetActive(false);
    8.         yield return new WaitForSeconds(2f);
    9.         Option_1.SetActive(true);
    10.     }
    11. }
     
  5. Shayke

    Shayke

    Joined:
    Dec 8, 2017
    Posts:
    352
    Did you hear about Invoke?
    Invoke("YourCommand", Time);
     
    Doug_B likes this.
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Oh, I know what they wanted, I just told them what they had. lol.
     
  7. CCouto96

    CCouto96

    Joined:
    May 18, 2016
    Posts:
    44
    Karrzun
    I applied your code but it still doesn't work. The button doesn't appear after the 2 seconds. There are no compiler errors either. Not sure what else to do.
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    The script has to be on another active gameobject. Make sure it's not on your button.
     
  9. CCouto96

    CCouto96

    Joined:
    May 18, 2016
    Posts:
    44
    Got it!
    It works now! Thank you everyone!
     
  10. CCouto96

    CCouto96

    Joined:
    May 18, 2016
    Posts:
    44
    Hey guys, I'm back again with one more question. Is there a way to change it so I can adjust the time it takes for the button to appear by changing the value in the Unity scene, rather than the script. Like, have one button appear after 2 seconds and have another appear after 3 seconds? Thanks.
     
  11. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    If you mean in the Editor, then you could add a line like this to your script:
    [SerializeField] [Range(1, 5)] int revealDelay = 3;
    and then set a delay (using your preferred technique) to wait for revealDelay seconds.

    Then you can change the delay value for each item that has that script in the Editor.
     
  12. CCouto96

    CCouto96

    Joined:
    May 18, 2016
    Posts:
    44
    Thanks!
     
    Doug_B likes this.
  13. EmmysGames

    EmmysGames

    Joined:
    Jan 22, 2021
    Posts:
    1
    Hi all, I used this Karrzun's edit to the code and all worked well. How would one set this up for multiple buttons aka add an option 2, option 3 etc.? I tried duplicating the script for a new button but that just led to compiling issues.

    Thank you and apologies for the beginner code question.
     
  14. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    Please don't respond to 3-year-old threads. Instead, start your own fresh question as per forum rules, even if you're using code from the old thread. Most likely it is a completely new problem.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand errors in general:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
    EmmysGames likes this.
  15. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If you literally make another copy of the script, you'll get compiler errors from multiple classes having the same name. What you probably want to do instead is either expand on the original script so it can take an arbitrary number of buttons instead of just 1, or just use multiple instances of the same script.
     
    EmmysGames likes this.
  16. TECNOJIGAMEROFFICIAL

    TECNOJIGAMEROFFICIAL

    Joined:
    Jun 16, 2021
    Posts:
    1
    but for me its like coming in first of game then after 3f it not visible i wanna do the opposite and loop it but how?
     
  17. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,599
    You are clearly failing to read existing information already posted here. I'll post it again for you:

    Please don't respond to 3-year-old threads. Instead, start your own fresh question as per forum rules, even if you're using code from the old thread. Most likely it is a completely new problem.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    How to understand errors in general:

    https://forum.unity.com/threads/ass...3-syntax-error-expected.1039702/#post-6730855

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/