- Home /
How to use List.ForEach Method?
I'm very confused as to how to use this method. I have this code:
var listTurrets = new List.<GameObject>();
I want to write a simple function that finds the closest gameObject in the list to the gameobject passed in as a parameter.
i know i can do this:
var indexToReturn;
var tempDistance;
for(i=0; i < listTurrets.Count;i++)
{
calculate distance, compare with previous,replace index if smaller distance.
}
This is easy cause i know "i" represents each object in the list as the loop iterates through it.
But for the inbuilt method listTurrets.ForEach() i dont see how to access the object in the list as the method iterates through them. I've been searching and searching and cant find a straight explanation anywhere...
Any assistance to put this confusion to rest is much appreciated :)
Answer by Vereos · Nov 27, 2013 at 10:53 PM
I guess you meant something like this:
for (var gameObj : GameObject in listTurrets) {
//your actions for each gameObj
}
Why are you insisting on this? That code will do exactly what you need it to. You can't use the ForEach function anyway because it requires language elements only available in C# (that being lambda functions), and from your code, you're using UnityScript.
@Hoeloe: Actually UnityScript also supports anonymous functions. In C# you don't have to use lambdas. Lambdas are just a way to create inplace closure-delegates.
You can do something like this in UnityScript:
var listTurrets = new List.<GameObject>();
// [...]
listTurrets.ForEach(function(obj:GameObject){print(obj.name);});
ForEach simply calls a function "for each" element in the list. It doesn't have to be an anonymous function. You can use any function with the right signature:
function doSomethingWithGO(obj : GameObject)
{
print("GO name is: " + obj.name);
}
listTurrets.ForEach(doSomethingWithGO);
In C# it works the same (with a slightly different syntax of course).
You're right, I apologise. Still, my point remains the same that it's fundamentally no different from a foreach loop.
Answer by AronChan · Nov 27, 2013 at 11:05 PM
This is how i approached something similiar:
// Target the first tower in the array of targets available to the tower
currentTarget = towerTargets[0];
// Calculate the distance from the tower to the target
float distanceToLastTarget = Vector3.Distance(this.gameObject.transform.position, currentTarget.transform.position);
// Find the closest enemy of the available targets and set it as the current target of the tower
for (int i = 1; i < towerTargets.Count; i++)
{
if ((Vector3.Distance(this.gameObject.transform.position, towerTargets[i].transform.position)) < distanceToLastTarget)
{
currentTarget = towerTargets[i];
}
}
It basicly sets the first "enemy" in the target list as the towers current target and then checks if any enemies are closer one by one. If there is an enemy that is closer that enemy replaces the current target. That way you will always come out of the for loop with the target that is closest to the tower.
lol yes that works, i was just looking for a solution using the ForEach function
i dont remember why i decided to go with the For loop (except they are faster than foreach) but there was some reasoning behind it.
if you insist on using the foreach i think it would go something like this:
foreach (GameObject target in towerTargets)
{
if ((Vector3.Distance(this.gameObject.transform.position, target.transform.position)) < distanceToLastTarget)
{
currentTarget = target;
}
}
xcept that doesnt work with a the list. it has its own method --> list.foreach()
It doesn't work because foreach
isn't valid in Javascript. The foreach loop in UnityScript is just written like this:
for (var target:GameObject in towerTargets)
No, because it's a C# method, not a UnityScript method, as I've already stated in a different comment. In C# you'd use it like this:
towerTargets.ForEach((t) => { //actions here });
Which is pretty much identical to use a foreach loop - except that the variable here is called "t" rather than "target". However, I don't believe UnityScript supports lambda functions (that's a function written like this: (args) => body
.
Your answer

Follow this Question
Related Questions
A node in a childnode? 1 Answer
For loop does not loop C# 2 Answers
How do I compare the position of my player to each elements position of an array of GameObjects? 0 Answers
Is there a way to organize tracking of collisions? 0 Answers
null reference exception when I already checked if this was null 1 Answer