fekj97
1
Hello everyone!
I’m trying to create a row of objects, one after another, but these objects are chosen randomly. My problem is how to create an object after another?
This is my current code:
void Generate(){
for (int i = 0; i < sections; i++) {
GameObject objs = Instantiate (objetos [Random.Range (0, objetos.Length)], transform.position, Quaternion.identy ) as GameObject;
}
}
Well, just add another line inside the for-loop, that sets the position however you want it, for example:
objs.transform.position = new Vector3(i*3, 0, 0);
PGJ1
3
If the objects have differents sizes, and you want them to align with each other, you can do something like this:
Vector2 position = transform.position;
for (int i = 0; i < sections; i++) {
GameObject go = objetos [Random.Range(0, objetos.Length)];
float width = go.GetComponent<Renderer>().bounds.size.x / 2;
position.x += width;
GameObject objs = Instantiate(go, position, Quaternion.identity ) as GameObject;
position.x += width;
}
fekj97
4
@JoeStrout @PGJ_1 Thank you both for the reply, the two codes work very well.
1 Like