Search Unity

Problem with for loop

Discussion in 'Scripting' started by Dev_Adrian_M, Apr 19, 2021.

  1. Dev_Adrian_M

    Dev_Adrian_M

    Joined:
    Jul 24, 2018
    Posts:
    1
    I have a problem where I enable 'helper UI' for each trigger. The trigger enabling works and the UI is not seen on the floor where the elevator is, which is what I want. The problem comes when I try to disable the 'helper UI', it seems to only work on the last floor, it won't even appear on the other floors. (The last floor is at i = 2)

    Here is the code snippet of the loop:

    Code (CSharp):
    1.         for (int i = 0; i < triggerPoints.Length; i++)
    2.         {
    3.             if (!triggerPoints[i].isNearElevator)
    4.             {
    5.                 helper.enabled = false;
    6.             }
    7.             else
    8.             {
    9.                 helper.enabled = true;
    10.                 helper.transform.localPosition = triggerPoints[i].transform.position + offset;
    11.  
    12.                 if (playerController.CallButton(1)) //Call elevator to the current floor
    13.                 {
    14.                     elevatorFloor = i;
    15.                 }
    16.  
    17.                 if (i == elevatorFloor)
    18.                 {
    19.                     helper.enabled = false; //Disable the helper when on the same floor as elevator
    20.                 }
    21.             }
    22.  
    23.             transform.position = Vector3.MoveTowards(transform.position, //Move the elevator to the selected floor
    24.                 new Vector3(transform.position.x, triggerPoints[elevatorFloor].transform.position.y, transform.position.z), elevatorSpeed * Time.deltaTime);
    25.         }
    Theoretically it supposed to work? since I have an if statement at the start of the loop that checks if you are near any of the elevator triggers. Weird thing is if I remove that if statement at start, it will show accordingly but will not disappear.

    //Edit
    I'm attaching a small gif just to visualize my problem to get a better picture of what I'm trying to achieve:

    The 'helper UI' only appears on top as you can see, and not on any other empty floors.
     
    Last edited: Apr 19, 2021
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    It doesn't work because your for loop is doing what it's suppose to. When a loop is hit, it enters the loop and runs through the entire thing till complete unless it's in a coroutine with yield statements or you break out of it.

    But in your case, it's entering the loop and checking all values. Even if i=1 says to turn on the helper, because your loop continues, i=2 may tell it to turn off the helper. Thus, your helper will only ever be on or off based on what the last entry is. This is why it "works" when you're on the last platform, because that's the last thing your for loop compares to.

    You could consider breaking out of the for loop if the helper gets turned on, that way whatever i it gets turned on at, it turns on and stays on. That might work for it.

    Also, if you add Debug.Log calls to your code, you probably can figure some of this out in the future. Just a tidbit of info to help.
     
    Rohit_Thomas likes this.