- Home /
<List> wont generate correctly
I have this code that is supposed to Load all the gameobjects in the resource folder into an array, parse the information and then generate a List of objects from it.
The problem is when i try to loop through the array to create a List of objects (building types). The array hold the correct data, but the final List just has the final Object in each in every member of the List.
For example the List is supposed to come out like
a b c d
but instead it comes out like
d d d d
I hope this all makes since, here is my code, Ive been looking over it for 3 days now and i just cant come up with anything, I hope someone can help
here is the building type class just for your reference
public class Building : System.Object
{ public GameObject buildingModel; public string buildingName; public int buildingIndex; public Material buildingTexture; public Texture buildingIcon;
}
but here is the actual code that I cant seem to get working correctly
public void LoadFilesIntoArray() {
Building building = new Building();
Object[] buildingList = Resources.LoadAll(datasetName,typeof(GameObject)); //these fbx objects must be collasped to single mesh to work properly
for(int i = 0; i < buildingList.Length; i++)
{
building.buildingModel = buildingList[i] as GameObject;
building.buildingName = buildingList[i].name as string;
building.buildingTexture = building.buildingModel.renderer.material; //save the initial material
buildingPallete.Add(building);
}
}
void OnGUI() { foreach(Building b in buildingPallete) { Debug.Log(b.buildingName); } }
Answer by benni05 · May 01, 2011 at 05:28 PM
Put
Building building = new Building();
inside the for loop.
just to expand on that - it should be the first line inside the for loop
haha thanks so much thats all it took, makes me feel pretty dumb thats like the last thing i hadnt tried ;p