- Home /
The question is answered, right answer was accepted
list.Add() not working
I have a for loop, adding objects to a list. But it only works the first time, and then just stops functioning. Everything else in the loop works.
public IEnumerator spawnReal(){
for(int y = 0;y < 8;y++){
for(int x = 0;x < 8;x++){
Color color = abilitiesInLevel[level].levelImage.GetPixel(x,y);
if(color != Color.white){
GameObject obj = Instantiate(projectile,new Vector3((x - 4) * 0.6F,5,-6),Quaternion.identity) as GameObject;
orb Orb = obj.GetComponent<orb>();
Debug.Log(Orb.ToString());
orbs.Add(Orb);
Orb.durability = (int)Mathf.Round(color.r * 5.1F);
Orb.type = (int)Mathf.Round(color.g * 5.1F);
Orb.speed = (int)Mathf.Round(color.b * 5.1F);
}
}
yield return new WaitForSeconds(0.3F);
}
yield break;
}
Without code snippet people will not be able to help you.
Sorry, my answer was incorrect. Have you debugged orbs.Count? Looks like it should work.
The debug tells me that, all objects are add to the list. But this code only runs once
foreach(orb Orb in orbs){if(Orb != null){
Orb.Ability(spell,abilityGUI.position,angle);
Debug.Log("Triggered");
}
And if you remove the null check, does it through a null reference error?
Answer by deathbane · Apr 01, 2015 at 03:33 PM
Having analyzed my code several times, I found that because of forgetting a bracket the full loop was larger than I thought and included: "yield break."
Answer by sumeeton · Apr 01, 2015 at 02:25 PM
Are you sure that is your exact code? Coz the error is very simple. This code should throw compiler errors.
Check where you declare and initialize orb
Orb orb = obj.GetComponent<orb>();
Debug.Log(orb.ToString());
orbs.Add(orb);
orb.durability = (int)Mathf.Round(color.r * 5.1F);
orb.type = (int)Mathf.Round(color.g * 5.1F);
orb.speed = (int)Mathf.Round(color.b * 5.1F);
Variable type comes before variable name in the declaration. And you are using the Class name itself to manipulate values and add it to the list.
The class name is orb. I am manipulating values of Orb. No compiler errors are thrown.
@deathbane:
And here you have a classical example why classnames must start with a capital letter. Every C# programmer would assume that "orb" is a variable and "Orb" is a class because that's the general na$$anonymous$$g convention. You have reversed that convention here. And what's most dangerous: Your convention isn't consistant since "x", "y", "color", "obj" and "orbs" are variables and not classes.
So this answer certainly doesn't solve the issue. However i don't see an issue in the posted code. Are you sure that "level" doesn't change and that image you use isn't somehow manipulated? We need to know more about how and when this coroutine is called.
Thanks for the tip @Bunny83. I will keep that in $$anonymous$$d
Answer by Bunny83 · Apr 01, 2015 at 04:03 PM
I suspect that you have selected the "collapse" button in the console window. It will collapse the same debug.Log messages into one. So the code works fine, but you misinterpreted your debug results.