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): for (int i = 0; i < triggerPoints.Length; i++) { if (!triggerPoints[i].isNearElevator) { helper.enabled = false; } else { helper.enabled = true; helper.transform.localPosition = triggerPoints[i].transform.position + offset; if (playerController.CallButton(1)) //Call elevator to the current floor { elevatorFloor = i; } if (i == elevatorFloor) { helper.enabled = false; //Disable the helper when on the same floor as elevator } } transform.position = Vector3.MoveTowards(transform.position, //Move the elevator to the selected floor new Vector3(transform.position.x, triggerPoints[elevatorFloor].transform.position.y, transform.position.z), elevatorSpeed * Time.deltaTime); } 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.
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.