I have tried different codes and it doesn’t work at all… And i’m slowly going insane, becuase this simple one thing, shouldn’t be this difficult. Anyone out there that knows how to make Button appear after a sudden amount of time?
Any help will be appreciated
My code:
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;
[SerializeField]
private Button _myButtonChoice; //Is this even necessary?
private void Start()
{
//if (gameObject.activeInHierarchy)
// gameObject.SetActive(false);
Button _myButtonChoice = GetComponent<Button>();
_myButtonChoice.enabled = false;
StartCoroutine(ActivationRoutine());
}
private IEnumerator ActivationRoutine()
{
yield return new WaitForSeconds(_sec * Time.deltaTime);
Button _myButtonChoice = GetComponent<Button>();
_myButtonChoice.enabled = true;
}
it only changes the Button’s enabled state; however, the Image that displays the button’s graphics will still be visible. Try changing _myButtonChoice.gameObject.SetActive(true/false) instead of .enabled.
Just use _sec in your WaitForSeconds, not _sec * Time.deltaTime.
So, the first question is, is the script this is on active in the scene? Are you sure the code is running?
That being said, this isn’t really a fade in, it’s just enabling the button component.
This is a quick mock up and not perfect, but it just turns on the target gameobject. Note the script can’t be on an inactive object as it doesn’t run.
public class TurnOnAfterTime : MonoBehaviour
{
public GameObject target;
IEnumerator Start()
{
yield return new WaitForSeconds(1f);
target.SetActive(true);
}
}
If you want it to “fade in” you will need to access the image component on the button and lerp it’s alpha before enabling it, which may be what you are trying to do.
Thanks for the advice. But I tried that first but whenever i use it as a gameObject.SetActive, i 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:26)
My code:
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;
[SerializeField]
private Button _myButtonChoice; //Is this even necessary?
private void Start()
{
//if (gameObject.activeInHierarchy)
// gameObject.SetActive(false);
Button _myButtonChoice = GetComponent<Button>();
_myButtonChoice.gameObject.SetActive(false);
StartCoroutine(ActivationRoutine());
}
private IEnumerator ActivationRoutine()
{
Button _myButtonChoice = GetComponent<Button>();
yield return new WaitForSeconds(_sec);
_myButtonChoice.gameObject.SetActive(true);
}
}
DUDE
You did it, thanks for the help
There is a small problem; The text is still visible from the start. But I probably just have to use the same method for the text.