WaitForSeconds Issue Under While Loop In Void Method

using UnityEngine;
using System.Collections;

public class TurretShells : MonoBehaviour
{
    public GameObject turretEmitter;
    public GameObject shell;
    public float shellForce;

    void OnTriggerEnter(Collider CharacterController)
    {
        while (true)
        {
            StartCoroutine(haltTime());
            GameObject Temporary_Bullet_Handler;
            Temporary_Bullet_Handler = Instantiate(shell, turretEmitter.transform.position, turretEmitter.transform.rotation) as GameObject;
            Rigidbody Temporary_RigidBody;
            Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody>();
            Temporary_RigidBody.AddForce(transform.forward * shellForce);
            Destroy(Temporary_Bullet_Handler, 3.0f);

        }
    }

    IEnumerator haltTime()
    {
        yield return new WaitForSeconds(2);
    }
}

My goal is to get the while loop under “void OnTriggerEnter” to contain the WaitForSeconds function; I want it to execute “haltTime” through every pass of the loop to avoid an emitter from causing the application to freeze because of an overload of created prefabs. I have the “yield return new WaitForSeconds(2);” section set properly, but the “StartCoroutine(haltTime());” line is not having any effect on the application. If someone could help me set the WaitForSeconds coroutine starter to work in the while loop, that would be great.

This solved it: Make the method statements part of the IEnumerator’s function; add a bool variable for true or false state of being inside the trigger, then start and stop the function through “Start/StopCoroutine(IEnumeratorFunction())”. Here is my modified script:

using UnityEngine;
using System.Collections;

public class TurretShells : MonoBehaviour
{
    public GameObject turretEmitter;
    public GameObject shell;
    public float shellForce;
    bool inTrigger = false;

    void Update()
    {
        if (inTrigger == true)
        {
            StartCoroutine(triggerTurret_Shells());
        }
        else
        {
            StopCoroutine(triggerTurret_Shells());
        }
    }

    void OnTriggerEnter(Collider CharacterController)
    {
        inTrigger = true;
        Debug.Log("true set");
    }

    void OnTriggerExit(Collider CharacterController)
    {
        inTrigger = false;
        Debug.Log("false set");
    }

    IEnumerator triggerTurret_Shells()
    {
        yield return new WaitForSeconds(0);
        GameObject Temporary_Bullet_Handler;
        Temporary_Bullet_Handler = Instantiate(shell, turretEmitter.transform.position, turretEmitter.transform.rotation) as GameObject;
        Rigidbody Temporary_RigidBody;
        Temporary_RigidBody = Temporary_Bullet_Handler.GetComponent<Rigidbody>();
        Temporary_RigidBody.AddForce(transform.forward * shellForce);
        Destroy(Temporary_Bullet_Handler, 1f);
    }
}

Change the “0” in “yield return new WaitForSeconds(0);” to whatever integer, float, or stored variable you need.

1 Like