- Home /
Spawn List of game Objects
I am trying to spawn a list of GameObjects. I have been roaming the documentation and I have tried to do this:
Currently I am getting out of bounds argument. Could anyone point me in the right direction? Thanks in advance.
public void spawntiles()
{
for (int i = 0; i < int_tile_spawnrate; i++)
{
int f = i - 1;
Vector2 previous_tile_location;
if (i == 0)
{
previous_tile_location = screenPosition;
array_tiles.Add(Instantiate(Tiles, previous_tile_location, Quaternion.identity) as GameObject);
}
else
{
previous_tile_location = array_tiles[f].transform.position + array_tiles[f].GetComponent<Collider>().bounds.size;
// var vector_previous_tile_position =array_tiles[f].transform.position;
// vector_previous_tile_position.x = vector_previous_tile_position + Tiles.GetComponent<Collider>().bounds.size;
}
}
}
Answer by Anton-Korhonen · Oct 31, 2016 at 10:10 PM
I would first store the reference to previous_tile_location outside the loop. Then inside your for-loop i would first spawn your Tiles object and then assign the previous_tile_location. This way every time the for-loop iterates, the object is instantiated to the location set in the last loop.
Something like this (untested):
previous_tile_location = screenPosition;
for (int i = 0; i < tile_spawnrate; i++)
{
array_tiles.Add(Instantiate(Tiles, previous_tile_location, Quaternion.identity) as GameObject);
previous_tile_location = array_tiles[i].transform.position + array_tiles[i].GetComponent<Collider>().bounds.size;
}
And if you want append the width of your collider to the position, you could use something like:
previous_tile_location = array_tiles[i].transform.position + new Vector3(array_tiles[i].transform.position.x + array_tiles[i].GetComponent<Collider>().bounds.size.x, array_tiles[i].transform.position.y, array_tiles[i].transform.position.z));
Oh that horrible forum code formatting.
Answer by EpicPants · Oct 31, 2016 at 10:58 AM
You don't add to your array in the else statement (where i!=0) so when i ==2 f will be 1 but your array count will be just 1. Basically you only add to your array when i == 0.
That is one of the issues, thanks for helping out. However, I get nothing spawned on the screen at all either. Could you tell me why?
Do they appear in the hierarchy? If so it may be an issue with positioning or scaling. I can't see any problems with your code at a glance but i'm not in a position to test it.
Thank you for re$$anonymous$$ding me of the hierarchy. It was spawning, but it was spawning away from the screen.
Sorry to bother, but how would you get the width of the previous gameobject in the list?
Answer by bishop87 · Oct 31, 2016 at 03:16 PM
try changing to i < int_tile_spawnrate to i < int_tile_spawnrate.length
Remove int f = i-1 and everyhwere that you have f on line 16/17 replace it with i.
also change if (i == 0) to if (i = 0)
Thank you for answering. The thing is that I need to get the previous gameobject on the list's position and add it's length in order to spawn the next object in front of it.
Well the part of the int f=i-1
will cause an out of bounds exception because the first time it loops it will be equal to -1 (arrays start at 0).
@bishop87 this is C# so if (i == 0)
is correct, if(i=0)
will throw an error because you are trying to assign 0 to i ins$$anonymous$$d of comparing the value.
@JincSoft Ah yes you're right. I sometimes get confused about which is a value comparison and which one is a bool comparison in IF statements