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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ButtonAppearance : MonoBehaviour {
public GameObject Option_1;
// Use this for initialization
void Start () {
StartCoroutine(ShowAndHide(Option_1, 2.0f) );
}
// Update is called once per frame
void Update () {
}
IEnumerator ShowAndHide(GameObject Option_1, float delay)
{
Option_1.SetActive(true);
yield return new WaitForSeconds(delay);
Option_1.SetActive(false);
}
}
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.
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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ButtonAppearance : MonoBehaviour {
public GameObject Option_1;
// Use this for initialization
void Start () {
StartCoroutine(HideAndShow(2.0f) );
}
// Update is called once per frame
void Update () {
}
IEnumerator HideAndShow(float delay)
{
Option_1.SetActive(false);
yield return new WaitForSeconds(delay);
Option_1.SetActive(true);
}
}
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. :
public class ButtonAppearance : MonoBehaviour
{
public GameObject Option_1;
IEnumerator Start()
{
Option_1.SetActive(false);
yield return new WaitForSeconds(2f);
Option_1.SetActive(true);
}
}
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.
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.
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.
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.
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:
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.
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: