I have 20 UI buttons that I need to appear one after other. The player pushes a button, it is destroyed and then so many seconds later another one appears, but only after the first one is destroyed. This process repeats for 20 buttons and then is looped. I have been using the included code to make the buttons appear after so many seconds. I’m wondering how to modify it so that it works as I described and then loops.
Thank you kindly in advance!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ButtonAppearance : MonoBehaviour
{
public GameObject Option_1;
public GameObject Option_2;
public GameObject Option_3;
public GameObject Option_4;
public GameObject Option_5;
public GameObject Option_6;
public GameObject Option_7;
public GameObject Option_8;
public GameObject Option_9;
public GameObject Option_10;
// Use this for initialization
void Start()
{
StartCoroutine(HideAndShow(Option_1, 3.0f));
StartCoroutine(HideAndShow(Option_2, 7.0f));
StartCoroutine(HideAndShow(Option_3, 11.0f));
StartCoroutine(HideAndShow(Option_4, 15.0f));
StartCoroutine(HideAndShow(Option_5, 19.0f));
StartCoroutine(HideAndShow(Option_6, 23.0f));
StartCoroutine(HideAndShow(Option_7, 27.0f));
StartCoroutine(HideAndShow(Option_8, 31.0f));
StartCoroutine(HideAndShow(Option_9, 35.0f));
StartCoroutine(HideAndShow(Option_10, 39.0f));
}
// Update is called once per frame
void Update()
{
}
IEnumerator HideAndShow(GameObject Option, float delay)
{
Option.SetActive(false);
yield return new WaitForSeconds(delay);
Option.SetActive(true);
}
}
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class ButtonAppearance : MonoBehaviour
{
// Fill the values in the inspector
public DelayedButton[] DelayedButtons;
void Start()
{
for(int i = 0 ; i < DelayedButtons.Length ; ++i)
{
DelayedButton nextButton = DelayedButtons[(i +1) % DelayedButtons.Length];
DelayedButtons*.ActiveGameObjectAfterDelayUponClick(nextButton, this);*
if (i > 0) DelayedButtons*.DisableButtonObject();* } DelayedButtons[0].ActiveGameObjectAfterDelay(DelayedButtons[0], this); } }
[System.Serializable] public class DelayedButton { [SerializeField] private Button button; [SerializeField, Min(0)] private float delay;
public void DisableButtonObject() { button.gameObject.SetActive(false); }
Thanks again @Hellium your script works great. I’ve been tinkering with it trying to make the first button have a delay the first time the game is run. As of now it appears immediately and only has a delay once the loop is complete. I tried putting the whole thing into a coroutine but it didn’t work great. Any advice on how to delay that first button the first time the script is run? aka delay the script from running say 3 seconds from the game opening?
Hi again @Hellium. I’ve noticed one strange thing where if there is more than 9 delayed buttons running in the game, the buttons start appearing two at a time. Anything in the code come to mind that could be causing this? Thank you in advance!
Hi again @Helium thanks once again for helping me with this problem. Could you highlight which part of the script causes the script to loop back to the first button once it is finished? I’m curious how that part functions for future use on other scripts. Thanks.
This script is great! Just what I needed for my project!
Thank you @Hellium. Is there any way to add a functionality where one button can activate more than one new button?
I would like for two (or more) new buttons to get activated and deactivated sumultaniously. Like they would be part of the same elements in the inspector. Is that possible?