Search Unity

Question How can I go through an array of colliders

Discussion in 'Scripting' started by Daniel_Rusu25, Feb 9, 2024.

  1. Daniel_Rusu25

    Daniel_Rusu25

    Joined:
    Jan 5, 2024
    Posts:
    16
    I have a script in which i have an array made out of some box colliders and i want each time I have the player enter each one in order to be fine, but if the player skips one collider to throw an error and if it gets to the end of the array, to get back to the top of the array. This is the script I am using:

    Code (CSharp):
    1.  
    2. if (System.Array.IndexOf(TrackCheckpoints.checkpoints, transform) == currentCheckpointIndex)
    3.             rightCheckpoint = true;
    4.         else
    5.             rightCheckpoint = false;
    6.  
    7.         if (rightCheckpoint == true)
    8.             currentCheckpointIndex++;
    9.  
    10.  
    11.         if (currentCheckpointIndex > TrackCheckpoints.checkpointTotalNumber)
    12.             currentCheckpointIndex = 1;
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    Steps to success:

    - keep an idea of what the next collider should be (such as an integer index into your array)

    - when the player hits a collider in this list:

    --> if it is the correct (or next) one, advance the counter
    --> if it is the wrong one, reset the counter

    - if at any time the counter is advanced and already on the last one, then wrap back to the first.

    This is just basic waypointing. Start with any one of ten billion Youtube tutorials on the idea. They are all going to essentially embody the above core concepts.