Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

(Solved) Next in Array

Discussion in 'Scripting' started by ForceX, Jul 18, 2010.

  1. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
    I'm trying to make a target next enemy script where the player hits a key and the script will select the next object with the tag "Enemy". As a test i have 3 cubes (cube 1, cube 2, cube 3) with the "Enemy" tag, and so far i can only get it to select cube 2. Any Ideas or suggestions are appreciated.


    Code (csharp):
    1. var Target : GameObject;
    2.  
    3. function TargetNextEnemy() : GameObject {
    4. var EnemyList : GameObject[];
    5. EnemyList = GameObject.FindGameObjectsWithTag("Enemy");
    6.    
    7.    for (i = 0; i <EnemyList.length; i++) {
    8.       if (EnemyList [i] == Target  i != EnemyList.length) {
    9.         Target = EnemyList [i++];
    10.         Debug.Log("target");  
    11.         }
    12.         else Target = EnemyList [i++];
    13.         }
    14. return Target;
    15. }
     
  2. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
    I've added some debug lines and i'm seeing something intersting. When i hit my next target button it outputs the Debug.Log 3-5 times per button down, but the code still does not pass on to the next object tag of "Enemy".


    Code (csharp):
    1. var Target : GameObject;
    2. private var EnemyList : GameObject[];
    3. var i=0;
    4.  
    5. function TargetNextEnemy() : GameObject {
    6.  
    7. EnemyList = GameObject.FindGameObjectsWithTag("Enemy");
    8.    
    9.    for (i=0; i <EnemyList.length; i++) {
    10.       if (EnemyList[i] == Target  i < EnemyList.length - 1) {
    11.         Target = EnemyList[i++];
    12.         Debug.Log("target++");
    13.         break;
    14.       }
    15.       else Target = EnemyList [0];
    16.       Debug.Log("target00");
    17.     }
    18.     return Target;
    19. }
    20.  
    21. function Update(){
    22.  
    23.     if (Input.GetButton("Next Target")){
    24.         TargetNextEnemy();
    25.     }
    26.  
    27. }
     
  3. Jesse Anders

    Jesse Anders

    Joined:
    Apr 5, 2008
    Posts:
    2,857
    It looks to me like the logic in your function is all wrong.

    First of all, the second part of the conditional ('i != EnemyList.length') serves no purpose, at least as far as I can see. If 'i' were >= EnemyList.length at this point, the loop would have exited already (and even if it hadn't, the array access in the first part of the conditional would be invalid, likely causing an error).

    It also looks like you're doing the same thing regardless of the result of the conditional - assigning EnemyList to Target - which means that the conditional serves no purpose.

    Finally, you're incrementing 'i' inside the loop, which means that the array will not be iterated over correctly.

    If you step through the logic, here's what happens:

    - At i = 0, EnemyList[0] is assigned to Target, and then i is incremented to 1.
    - i is incremented to 2 at the end of the loop.
    - At i = 2, EnemyList[2] is assigned to Target, and then i is incremented to 3.
    - The loop terminates.

    As such, the result will always be EnemyList[2], exactly as you're observing.

    Try something like this instead (pseudocode):

    Code (csharp):
    1. int current = 0;
    2. for (int i = 0; i < EnemyList.length; ++i) {
    3.     if (EnemyList[i] == Target) {
    4.         current = i;
    5.     }
    6. }
    7. current = (current + 1) % EnemyList.length;
    8. Target = EnemyList[current];
     
  4. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
    Jesse Anders. Thank you for taking the time and explain it. I see what your talking about and I'll be able to build off of this in the future.


    For any one that is interested here is the complete code to select a target with the tag "Enemy" and to draw a GUI Texture over your currently selected target.

    I'm sure it's not perfect by any means but it works.


    CODE UPDATED ON http://forum.unity3d.com/viewtopic.php?p=358792#358792
     
  5. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
  6. Meliodass

    Meliodass

    Joined:
    Feb 8, 2015
    Posts:
    32
    hi ive seen this page and i want to learn more about this but your link is broken can i ask if the topic is still there?
     
    Z0leee87 likes this.