- Home /
Question by
Supphakon · Apr 14, 2020 at 02:30 PM ·
c#unity 2dupdate functionstartcoroutinecoroutine errors
StartCoroutine not working in Update!!!
StartCoroutine not working in Update!!!
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class RandimNumber : MonoBehaviour
{
public Text text;
public Text timer;
public Text t;
public int number;
int button;
float position;
float showtime;
float pausetime;
private bool keyuse;
private bool textuse;
private bool playtime;
float time;
float step;
void Start()
{
StartCoroutine(TimerWait());
showtime = 3;
pausetime = 0;
button = 99;
textuse = false;
step = 1;
time = 0;
playtime = false;
}
// Update is called once per frame
void Update()
{
if (playtime == true) // --------------this isn't working
{
Debug.Log("StartCoroutine(Wait);");
StartCoroutine("Wait"); // --------------this isn't working (StartCoroutine )
playtime = false;
}
if (playtime == false)
{
Debug.Log("StopCoroutine(Wait);");
StopCoroutine("Wait");
}
if (time == 30)
{
StopAllCoroutines();
Debug.Log("gameOver");
textuse = false;
keyuse = false;
}
if (textuse == true)
{
Debug.Log("Textuseใช้ได้");
if (text.text.Length == t.text.Length)
{
playtime = false;
if (text.text == t.text)
{
time = 0;
pausetime = 0;
button = 99;
StopAllCoroutines();
Debug.Log("StartCoroutine(TimerWait())");
StartCoroutine(TimerWait());
textuse = false;
}
if (text.text != t.text)
{
StopAllCoroutines();
Debug.Log("gameOver");
textuse = false;
keyuse = false;
}
}
if (text.text.Length < t.text.Length)
{
StopAllCoroutines();
Debug.Log("gameOver");
textuse = false;
keyuse = false;
}
}
if (keyuse == true) {
Debug.Log("Key useใช้ได้");
if (Input.GetKeyDown(KeyCode.Keypad0))
{
button = 0;
t.text = t.text + button.ToString();
textuse = true;
Debug.Log("T=" + t.text);
Debug.Log("button = 0");
}
if (Input.GetKeyDown(KeyCode.Keypad1))
{
button = 1;
t.text = t.text + button.ToString();
textuse = true;
Debug.Log("T=" + t.text);
Debug.Log("button = 1");
}
}
if (keyuse == false) {
Debug.Log("Key useใช้ไม่ได้");
}
}
IEnumerator TimerWait() {
keyuse = false;
yield return new WaitForSeconds(0.1f);
text.enabled = true;
textuse = false;
t.text = "";
number = Random.Range(0, 2);
text.text = text.text + number.ToString();
Debug.Log("รอ3วิ");
yield return new WaitForSeconds(showtime);
text.enabled = false;
yield return new WaitForSeconds(pausetime);
keyuse = true;
playtime = true;
}
IEnumerator Wait()
{
time = 0;
while (time < 30)
{
yield return new WaitForSeconds(step);
time = time + step;
Debug.Log(time);
}
}
}
Comment