Question by
kooljay68 · Nov 29, 2018 at 03:42 AM ·
instantiatebuttondestroy
I want to Instantiate a force field - let it work for x(sec) show countdown and then make wait for a count up - but then my button doesn't seem to work anymore?
Here is my code:`using UnityEngine; using UnityEngine.UI; using System.Collections; using TMPro;
public class CountDown : MonoBehaviour { float timeLeft = 10; float waittime = 10;
private string T;
public GameObject forcefield;
public bool ffield = false;
public bool ffcount = false;
public Button button;
private TMP_Text m_TextComponent;
private GameObject _instance;
private void Start()
{
button.onClick.AddListener(TaskOnClick);
Debug.Log("STARTED LISTENING)");
}
void Update()
{
if (ffield == true && ffcount == false)
{
timeLeft -= Time.deltaTime;
m_TextComponent = GetComponent<TMP_Text>();
T = Mathf.Round(timeLeft).ToString("0");
m_TextComponent.text = T;
if (timeLeft < 0)
{
Destroy(_instance);
ffield = true;
ffcount = true;
Debug.Log("true - true");
}
}
if (ffield == true && ffcount == true)
{
waittime -= Time.deltaTime;
m_TextComponent = GetComponent<TMP_Text>();
T = Mathf.Round(waittime).ToString("0");
m_TextComponent.text = T;
if (waittime < 0)
{
ffield = false;
ffcount = false;
Debug.Log("false - false");
button.onClick.AddListener(TaskOnClick);
Debug.Log("STARTED LISTENING)");
}
}
}
void TaskOnClick()
{
if (ffield == false && ffcount == false)
{
_instance = Instantiate(forcefield);
ffield = true;
Debug.Log("Turn On");
}
}
} `
Comment