- Home /
Make Button appear after a sudden amount of time
I'm making a Interactive Film. Everything is working, i just want my buttons to appear after a sudden amount of time. I made script, that i have attached to my buttons, but when I play the game the button won't appear again. I just get this error: Coroutine couldn't be started because the the game object 'Choice_1' is inactive! UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) Button_Fade_In:Start() (at Assets/Scripts/Button_Fade_In.cs:19)
I wan't the time factor to be adjustable in the inspector.
My Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class Button_Fade_In : MonoBehaviour
{
[SerializeField]
private float _sec;
public GameObject _objectToActivate;
private void Start()
{
if (gameObject.activeInHierarchy)
gameObject.SetActive(false);
StartCoroutine(ActivationRoutine());
}
private IEnumerator ActivationRoutine()
{
yield return new WaitForSeconds(_sec * Time.deltaTime);
gameObject.SetActive(true);
}
,I'm making a Interactive film for a school project. I already made 2 buttons that can start a new video clip, but the problem is the button is visible and clickable from the start of every video. I want the button to appear after a sudden amount of time. And I also want the time to be adjustable in the inspector.
I'm a noob at C# using System.Collections; using System.Collections.Generic; using UnityEngine.UI; using UnityEngine;
public class Button_Fade_In : MonoBehaviour
{
[SerializeField]
private float _sec;
public GameObject _objectToActivate;
private void Start()
{
if (gameObject.activeInHierarchy)
gameObject.SetActive(false);
StartCoroutine(ActivationRoutine());
}
private IEnumerator ActivationRoutine()
{
yield return new WaitForSeconds(_sec * Time.deltaTime);
gameObject.SetActive(true);
}
I made this script but it doesn't work... Whenever i click Play, my buttons won't appear again. I got this error: Coroutine couldn't be started because the the game object 'Choice_1' is inactive! UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator) Button_Fade_In:Start() (at Assets/Scripts/Button_Fade_In.cs:19)
Run the coroutine on a different gameobject. I suggest calling it Ui$$anonymous$$anager.
Also, remove the * Time.deltaTime from the WaitForSeconds parameter, it is not required.
Answer by talyh · Sep 24, 2019 at 06:59 PM
I think you meant to have _objectToActivate.SetActive(true)
(same in your Start
. The current code disables the gameObject, so that coroutine won't run.
(Side note: not sure if this is attached to the button itself. if it is, you can keep the gameObject active but not visible, setting the button alpha to 0 until it's time to show, or using a CanvasGroup to hide multiple objects).
Answer by Tarball · Sep 24, 2019 at 10:43 PM
What I would do is make a parent object with child button objects in its hierarchy. Attach your script to the parent object (which is always active) and turn your button objects on and off, while your script continues running. Also, one more thing. Consider using something besides the unreliable WaitForSeconds. Try this instead:
bool myBool;
yield return new WaitUntil(() => myBool);
That will wait until myBool == true before executing subsequent code.
Answer by surfuay · Sep 25, 2019 at 01:30 AM
you've got a lot of good answers.
personally i would just make a coroutine and call it in the start method. in the coroutine just state how long you want to wait.
ie
void start ()
{
startcoroutine(buttonappear());
}
ienumerator buttonappear()
{
yield return new waitforseconds(90);
button.setactive(true);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612212553im_/https://answers.unity.com/themes/thub/images/avi.jpg)