Question by
cadeautje · Dec 01, 2015 at 11:38 AM ·
c#instantiateprefabfor loop
start position instantiate for loop. c#
I am trying to stack up a number of objects on top of each other using a for loop and instantiating them with a prefab, this all works fine but I have 1 problem. This is the code I use:
using UnityEngine;
using System.Collections;
public class SpawnScript : MonoBehaviour
{
public GameObject TargetPrefab;
void Start ()
{
for (int i = 0;i < 5; i++)
{
Instantiate(TargetPrefab, new Vector3(i * 0.9f, 0f, 0f), Quaternion.identity);
}
}
}
this works fine and it makes the 5 cubes I want with the 0.9 offset, but then I want to make another layer on top of it with the same 0.9 offset only then I want it to start more to the right because I want to stack the objects like a pyramid, but this way it always starts at 0, how do I set the start position to anything I want, while keeping the offset at 0.9?
Comment
Best Answer
Answer by mikelortega · Dec 01, 2015 at 12:28 PM
Do you mean something like this?
void Start()
{
for (int i = 0; i < 5; i++)
Instantiate(TargetPrefab, new Vector3(i * 0.9f, 0f, 0f), Quaternion.identity);
for (int i = 0; i < 4; i++)
Instantiate(TargetPrefab, new Vector3(i * 0.9f + 0.45f, 0.9f, 0f), Quaternion.identity);
}
Omg yes that is exactly what I need how did I miss that I'm so stupid xD. Thanks a lot ^^
Your answer
