Unity C# | Problem Using For Loop Inside Update()

Asked

Viewed 71 times

0

Hello,

My problem is this, I have a list of events that happen at a certain time, which is randomized, and when an event occurs a new timer is drawn to the next one adding another 10 seconds, all events are being called, the problem is that apparently for is skipping the index a few times, for example, the list is {"TV","Carro","Vaso", "Armario"} the first event to be called should be the TV, but in fact is being called the Vase, then the Armory, then the TV and the Car. I believe it is something involving the amount of frames called in the update, but I could not find anything that solved.

Thank you.

void Update()
    {

        Check = EventTrigger.Check;

        if(FinalEvents != null)
        for(int i = 0; i < FinalEvents.Count; i++)
        {

            if(FinalEvents[i] == "TV" && Check)
            {
                Timer -= Time.deltaTime;
                print(Timer);

                if (Timer <= 0)
                {
                    print("TV");
                    video.Play();
                    distances.CheckFear(GameObject.Find("Cenario/TV"), GameObject.Find("Player"), slider);
                    Timer = Random.Range(1, 2) + 10f;
                    i++;
                }

            }else

            if (FinalEvents[i] == "Carro" && Check)
            {
                Timer -= Time.deltaTime;
                print(Timer);

                if (Timer <= 0)
                {
                    print("Carro");
                    anim.SetTrigger("btn1");
                    distances.CheckFear(GameObject.Find("Cenario/Carrinho"), GameObject.Find("Player"), slider);
                    Timer = Random.Range(1, 2) + 10f;
                    i++;
                }

            }else

            if (FinalEvents[i] == "Armario" && Check)
            {
                Timer -= Time.deltaTime;
                print(Timer);

                if (Timer <= 0)
                {
                    print("Arm");
                    distances.CheckFear(GameObject.Find("Cenario/guardaroupas"), GameObject.Find("Player"), slider);
                    Timer = Random.Range(1, 2) + 10f;
                    i++;
                }

            }else

            if (FinalEvents[i] == "Vaso" && Check)
            {
                Timer -= Time.deltaTime;
                print(Timer);

                if (Timer <= 0)
                {
                    print("Vaso");
                    distances.CheckFear(GameObject.Find("Cenario/vaso"), GameObject.Find("Player"), slider);
                    Timer = Random.Range(1, 2) + 10f;
                    i++;
                }

            }

        }
}
  • 1

    If the idea was to have a counter that fires the event after a certain time, just put the line Timer -= Time.deltaTime; before the loop. Now I see a problem with the event list, is it fixed? How is it filled? Because vc makes a loop and obligatorily, if the list is fixed in the form you have passed, it will always pass in the four events (and consequently subtracting the time counter for at least four times in a single frame)

  • I guess I get it, so the for goes into the four events because it completes the whole loop frame, right? I want to call the next item on the list only when the first timer ends, would using a foreach be more effective? PS: The list is fixed, but is randomized in Start()

1 answer

1


His method Update() needs to know which event will run beforehand, my suggestion is to create a variable that controls which event will run, follows code:

int proxEvento = 0; // inicializa para o primeiro item da lista de eventos
void Update()
{

    Check = EventTrigger.Check;
    Timer -= Time.deltaTime;
    if (Timer <= 0)
    {
        if(FinalEvents != null && proxEvento < 4 /*Aqui vc vai querer trocar o 4 pelo Length ou Count (não sei se FinalEvents é Array ou List<>*/)
        {
            if(FinalEvents[proxEvento] == "TV" && Check)
            {
                print(Timer);

                print("TV");
                video.Play();
                distances.CheckFear(GameObject.Find("Cenario/TV"), GameObject.Find("Player"), slider);
                Timer = Random.Range(1, 2) + 10f;
                proxEvento++;
            }else

            if (FinalEvents[proxEvento] == "Carro" && Check)
            {
                print(Timer);

                print("Carro");
                anim.SetTrigger("btn1");
                distances.CheckFear(GameObject.Find("Cenario/Carrinho"), GameObject.Find("Player"), slider);
                Timer = Random.Range(1, 2) + 10f;
                proxEvento++;                

            }else

            if (FinalEvents[proxEvento] == "Armario" && Check)
            {
                print(Timer);

                print("Arm");
                distances.CheckFear(GameObject.Find("Cenario/guardaroupas"), GameObject.Find("Player"), slider);
                Timer = Random.Range(1, 2) + 10f;
                proxEvento++;
            }else

            if (FinalEvents[proxEvento] == "Vaso" && Check)
            {
                print(Timer);

                print("Vaso");
                distances.CheckFear(GameObject.Find("Cenario/vaso"), GameObject.Find("Player"), slider);
                Timer = Random.Range(1, 2) + 10f;
                proxEvento++;
            }
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.