- Home /
For Loop Not Iterating Properly?
This one is strange. I've got a for loop set up to add each gameObject of an array to another array. I have a print check to make sure it's working, so I know the for loop is iterating, but it keeps on adding the same gameObject to the array. It makes absolutely zero sense.
var newTargets : List.<TargetClass>;
var nullTarget : TargetClass;
public class TargetClass{
var target : GameObject;
var threat : float;
}
function Start(){
FindTarget();
}
function FindTarget(){
for(var p : GameObject in GameObject.FindGameObjectsWithTag("Friendly")){
nullTarget.target = p;
print(nullTarget.target);
newTargets.Add(nullTarget);
}
}
So let's say we have two objects called "Man 01" and "Man 02." If we try to add them to newTargets, it will just add "Man 02" twice, yet it will print "Man 01", and then "Man 02." So the nullTarget says that it's switching, but when I check the editor, it hasn't.
Somebody please help me. This is driving me insane!
Answer by Bunny83 · Jul 27, 2012 at 12:34 PM
You have to create an instance of your TargetClass for each target. It's a reference type. If you just create one instance, you will be using this one instance all the time and add the same reference to your List.
Btw: nullTarget i badly worded and shouldn't be a member variable.
public class TargetClass
{
var target : GameObject;
var threat : float;
}
var newTargets : List.<TargetClass>;
function Start()
{
FindTarget();
}
function FindTarget()
{
for(var p : GameObject in GameObject.FindGameObjectsWithTag("Friendly"))
{
var item = new TargetClass();
item.target = p;
print(item.target);
newTargets.Add(item);
}
}
I almost want to meet you just so I can hug you. Thank you so much. Not only did you answer my question, but explained why my process is wrong and taught me something in the process. Few people on here manage to pull that off. Thanks again, Bunny. Cheers!
You're welcome! It's not really something special ;) Just some basic OOP.
Answer by Seth-Bergman · Jul 27, 2012 at 12:22 PM
newTargets.Add(nullTarget.target); }
you're setting the "target" data member of nullTarget, but never changing nullTarget itself, just it's data member "target"
but on the last line you just add nullTarget, should be nullTarget.target I guess
EDIT:
function FindTarget(){
for(var p : GameObject in GameObject.FindGameObjectsWithTag("Friendly")){
nullTarget = p.GetComponent(TargetClass);
print(nullTarget);
newTargets.Add(nullTarget);
}
this way you are passing each object, instead of the same one each time
No because newTargets is a list of TargetClass, so adding just the target of nulltarget wouldn't work.
then you should be setting p = nullTarget.
you are only changing the nullTarget.target var, but nullTarget is still the same object..
see my edit above
TargetClass is a plain data class and not a $$anonymous$$onoBehaviour component. You spotted the problem, but your solution isn't quite right ;)
Now I see.. my eyes went straight to the bottom .. (still a bit harsh voting me down tho) :}
Your answer
![](https://koobas.hobune.stream/wayback/20220613074759im_/https://answers.unity.com/themes/thub/images/avi.jpg)