Hey guys, I want to make an enemy activate a Bang! sprite using a Random.Range, but i’ve fallen into a infinte loop I’m unable to break out of. Any help is appreciated. I am using IEnumerators to do it, I think its the right way but I’m not sure.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DisparoEnemigo : MonoBehaviour
{
public GameObject Bang;
[Range(0f, 10f)]
public float minTime = 2f;
[Range(0f, 10f)]
public float maxTime = 4f;
public float counter;
public bool bangActivo = false;
void Start()
{
counter = 5f;
}
void Update()
{
//Debug.Log(counter);
counter -= Time.deltaTime;
if (counter <= 0f)
{
//Prueba();
StartCoroutine(EsperarAlBang());
Debug.Log("if() 1 completado");
//counter = -1f;
}
if (bangActivo == true)
{
StartCoroutine(FueraBang());
Debug.Log("If() 2 completado");
}
}
IEnumerator EsperarAlBang()
{
yield return new WaitForSeconds(Random.Range(minTime, maxTime));
Bang.SetActive(true);
Debug.Log(“BangActivo true”);
bangActivo = true;
//Debug.Log(“Ienumerator 1 completado”);
}
IEnumerator FueraBang()
{
yield return new WaitForSeconds(5f);
Bang.SetActive(false);
bangActivo = false;
counter = 5f;
Debug.Log(“Ienumerator 2 completado”);
}
void Prueba()
{
}
}