I am using a list to hold the transform.position.x location of a bunch of game objects and then i want to go through the list with a for loop. However for some reason i keep getting an error saying ‘length’ is not a member of ‘System.Collections.Generic.List.’.
here is what i have
import System.Collections.Generic;
var spawnPositions : List.<float> = new List.<float>();
function SpawnBlocks(l : float){
spawnPositions.Add(l);
for (var i = 0; i < spawnPositions.length; i++){
if(spawnPositions[i] == 2){
// not doing anything here at the moment
}
Instantiate(numberBlock,Vector3(l,7.2+(count * blockSize),0),Quaternion.identity);
}
}
I tried using .Count but the for loop was giving me really weird results, if there was only 2 objects in the list it would spawn 3 blocks, and when there were 3 objects in the list it would spawn 6 and so on. and i’m not sure so i thought it was a problem with how Count works? sorry i’m new to using Lists.
Where are you calling SpawnBlocks? Count should give you the number of objects currently in the list. I’ve been using Count for a lot of things and haven’t encountered that problem.
Oh… i was having each object call it so the for loop was getting run multiple times. TY for the help
Hey, I know I’m a little bit late…but for new programmers: you have to note that C# is Case Sensitive language. You’ve written “length”, but it must “Length”!
You’re not a little late, you’re necroing a thread that is 10 years old. Please don’t do this.
https://discussions.unity.com/t/757848
1 Like