Target first object in an array before moving to next

Hey ! I’m creating a bounce attack feature like in sonic the hedgehog in which you can bounce attack one enemy and then another so looping through an array enemies.
So far I’ve got the bounce attack working for when it targets just one enemy without it looping.

However I’ve tried to implement a loop for the bounce attack to attack a loop of enemies
eg// target first closest enemy then the next closest etc.

But nothing actually happens no errors at all and it doesn’t target the closest enemy let alone the next .

I want to use loop rather than a ontrigger because i’m going to use the ontrigger for when the bounce attack collider with a sphere collider that spawns when enemy is in distance. Just in case your wondering.

Heres the code below for you to have an idea edited version of code i took out the array and i’m going to start looking at the answers you’ve provided :

       function BounceAttack()
     {
     
        var controller : CharacterController = GetComponent(CharacterController);
        var distance = Vector3.Distance(target.position,transform.position);

            
            
            if( jumpSpeed==70.0 && !controller.isGrounded )// if the jump speed if 70.0 and its not 
            // colliding with anything or not on the ground
            
             {
                  if(Input.GetButton("Fire2") && gravity ==90.0 && distance

Your indentation and structure is quite a bit hard to read. My best tip is to break everything out from the Update-loop so you can yield while waiting for an enemy to be bounced. You could have a look at my sorting of closest collider for a smaller array to work with which I believe would be more easy to maintain as it’s created and updated in realtime from your player’s position. You would call FindClosestCollider() when IsJumping() of the CharacterController returns true whilst Fire3 is pressed - if I understand your script correctly.

Then you could yield until linear interpolation of player towards target is done and finally until bounce happens, with a possibility to exit i presume by release of Fire3. Then just repeat while Fire3 is held and your other conditions returns true. Have a look at linear interpolation on a curve for instance.

Another thing is that you could cache controller outside Update to spare some performance power, cache it in Awake() or Start().

So best tip is, break it out into functions and think of it as a state machine where states happens after one another with certain conditions met. This way you can yield to wait for another state more efficient and see the states more clearly. Hope it helps!