I put this script in, this message pops up and I can't go next
Unity 2021.3.15f1
Windows 10
Project name: 포둥
application.enterplaymode/ wating for Unyty's code to finish excuting
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class podungsay : MonoBehaviour
{
public GameObject Textbox;
public bool sayingf;
public UnityEngine.UI.Text sayingtext;
public GameObject openmouth;
public GameObject closemouth;
IEnumerator OnType(float interval, string Say, float wait)
{
sayingf = true;
foreach (char item in Say)
{
sayingtext.text = item;
yield return new WaitForSeconds(interval);
}
yield return new WaitForSeconds(wait);
sayingf = false;
}
void Update()
{
if (sayingf == true)
{
Textbox.SetActive(true);
}
else
{
Textbox.SetActive(false);
}
}
IEnumerator mouth()
{
while (true)
{
while (sayingf == true)
{
openmouth.SetActive(false);
closemouth.SetActive(true);
yield return new WaitForSeconds(0.7f);
openmouth.SetActive(true);
closemouth.SetActive(false);
yield return new WaitForSeconds(0.7f);
}
openmouth.SetActive(true);
closemouth.SetActive(false);
}
}
void Start()
{
StartCoroutine(mouth());
}
}
So I changed the version to 2021.3.17f1 but still got this message What's wrong with the command? (Use Google translator)
CodePudding user response:
It is stuck because you are using a while
which never ends. Leading to an infinite loop.
Use the Update()
Method instead.
CodePudding user response:
Right here:
IEnumerator mouth()
{
while (true)
{
while (sayingf == true)
{
openmouth.SetActive(false);
closemouth.SetActive(true);
yield return new WaitForSeconds(0.7f);
openmouth.SetActive(true);
closemouth.SetActive(false);
yield return new WaitForSeconds(0.7f);
}
openmouth.SetActive(true);
closemouth.SetActive(false);
}
}
When "sayingf" is false, it will set openmouth to be active and closemouse to be inactive, and then it will repeat from the while (true)
, causing an infinite loop. You need to add a delay. You can use yield return 0
or yield return null
to wait for one frame to pass.
Also note that sayingf == true
is a sin. This is your warning. The next infraction, I will resort to violence.
when using bool == true
, either of these will happen:
true == true
which will become true
.
false == true
which will become false
.
You can just write if (sayingf)
as == true
isn't doing anything to the bool.