Почему булевая переменная не меняет свой статус?
пытаюсь сделать спейс-шутер на Unity и прописываю код и не могу понять в чем моя ошибка и почему булевая переменная не меняет свой статус при соприкосновении объектов с триггером и из-за этого дальше код не может срабатывать корректно. Что делать?
public bool detect = true;
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.CompareTag("Alien") && detect)
{
detect = false;
collision.GetComponentInParent<Wave>().WaveTouchBumper();
StartCoroutine(wait());
}
}
IEnumerator wait()
{
yield return new WaitForSeconds(0.2f);
detect = true;
}
Ответы (1 шт):
Автор решения: Blackmeser
→ Ссылка
protected bool detect = true;
public bool Detect => detect;
protected void SetDetect(bool _detect, string source)
{
detect = _detect;
string s = $"{this.имя_или_идентификатор} Detect by {source ?? string.Empty}";
Console.WriteLine(s); //или какая там консоль в унити
}
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.CompareTag("Alien") && detect)
{
SetDetect(false, "OnTriggerEnter2D");
collision.GetComponentInParent<Wave>().WaveTouchBumper();
StartCoroutine(wait());
}
}
IEnumerator wait()
{
yield return new WaitForSeconds(0.2f);
SetDetect(true, "IEnumerator wait()");
}