(Solved) Next in Array

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.

var Target : GameObject;

function TargetNextEnemy() : GameObject {
var EnemyList : GameObject[];
EnemyList = GameObject.FindGameObjectsWithTag("Enemy");
   
   for (i = 0; i <EnemyList.length; i++) { 
      if (EnemyList [i] == Target  i != EnemyList.length) { 
		Target = EnemyList [i++]; 
		Debug.Log("target");  
		}
		else Target = EnemyList [i++];
		}
return Target;
}

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”.

var Target : GameObject; 
private var EnemyList : GameObject[]; 
var i=0;

function TargetNextEnemy() : GameObject { 

EnemyList = GameObject.FindGameObjectsWithTag("Enemy"); 
    
   for (i=0; i <EnemyList.length; i++) { 
      if (EnemyList[i] == Target  i < EnemyList.length - 1) { 
		Target = EnemyList[i++]; 
		Debug.Log("target++");
		break;
      } 
      else Target = EnemyList [0]; 
	  Debug.Log("target00");
	} 
	return Target; 
}

function Update(){

	if (Input.GetButton("Next Target")){
		TargetNextEnemy();
	}

}

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):
* *int current = 0; for (int i = 0; i < EnemyList.length; ++i) { if (EnemyList[i] == Target) { current = i; } } current = (current + 1) % EnemyList.length; Target = EnemyList[current];* *

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

Updated script to v1.1
Moved script to the Showcase forum. You can check it out here:http://forum.unity3d.com/viewtopic.php?p=358792#358792

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?

1 Like