Question by
erqi · Oct 11, 2015 at 03:30 PM ·
c#buttonscript.scripting beginnerbuttonstates
How to enable and disable back game object for ui button after a few seconds?
Hello,
i`m beginer in programing or unity, i want to make script for enable and disable back after a few sec for UI button. i founded this script:
using UnityEngine;
using System.Collections;
public class ActiveStateToggler : MonoBehaviour {
public void ToggleActive () {
gameObject.SetActive (!gameObject.activeSelf);
}
}
but this script wil enable with 1 click and disable after i click again.
What i should add into script? WaitForSeconds?
ps: I used Unity 5.2 and i used C#
sorry for bad english.
Comment
Answer by Firedan1176 · Sep 28, 2016 at 05:14 PM
You'll need to use a Coroutine. You can use something like this:
public void ToggleActive() {
StartCoroutine(Reverse());
}
IENumerator Reverse() {
yield return new WaitForSeconds(1f); //Wait 1 second
gameObject.SetActive(!gameObject.activeSelf);
}