- Home /
loop stops after a while
i'm trying to create this script that activates 2 different functions depending on the distance between two objects, and it seems to work just fine the first few times, then it just stops
void Fire()
{
if (Vector3.Distance(amico.transform.position, nemico.transform.position) < 4)
{
keepgoing();
Debug.Log("cristo");
footpart.SetActive(true);
StartCoroutine(footparte());
GameObject tempBullet = Instantiate(bullet, transform.position, transform.rotation) as
GameObject;
Rigidbody tempRigidBodyBullet = tempBullet.GetComponent<Rigidbody>();
tempRigidBodyBullet.AddForce(tempRigidBodyBullet.transform.forward * bulletSpeed);
Destroy(tempBullet, 0.15f);
timestamp = Time.time + ShootRate;
Count = +1;
bulletAudio.Play();
}
else
{
Debug.Log("fail");
keepgoing();
}
}
void keepgoing()
{
StartCoroutine(keepgoing1());
Debug.Log("boh");
}
IEnumerator keepgoing1()
{
yield return new WaitForSeconds(Random.Range(2f, 3f));
actions[Random.Range(0,actions.Length)]();
Debug.Log("aaa");
Fire();
}
IEnumerator footparte()
{
yield return new WaitForSeconds(0.3f);
footpart.SetActive(false);
}
actions[Random.Range(0,actions.Length)]();
Can you post everything that is stored in the actions array?
Answer by ray2yar · Jul 04, 2020 at 10:45 PM
I tested your loop structure without all the other code and it works fine.... However, I'm curious as to why you use keepgoing() to start a coroutine- why not just start the coroutine from within that fire() loop without the additional function? Anyways your error is not the loop itself as far as I can tell.
Your answer

Follow this Question
Related Questions
audio synthesizer in unity? 1 Answer
Animation Keeps Looping 2 Answers
How To Wrap A Blender Animation??? 1 Answer
How to make player death sound effect / hitsound play once when function is called? 1 Answer
Animator is looping 1 Answer