- Home /
Question by
yusufalp727 · Jul 12, 2021 at 12:18 PM ·
instantiateclone
What can I do to make the objects I instantiated move with the background?
I created an empty object called SpawnManager. I wrote the code for instantiating and moving and added prefabs. But since the instantiated objects are not cloned as children of SpawnManager object, I cannot move them. What can I do to move the instantiated objects?
public GameObject[] obstaclePrefab;
private float startDelay=2;
private float repeatRate=2;
public float spawnPosX=5;
public float spawnPosY=15;
void Start()
{
InvokeRepeating("SpawnObstacle", startDelay, repeatRate);
}
void SpawnObstacle()
{
Vector3 spawnPos=new Vector3(Random.Range(-spawnPosX, spawnPosX),Random.Range(-spawnPosY,spawnPosY),-5);
int spawnIndex = Random.Range(0,obstaclePrefab.Length);
Instantiate(obstaclePrefab[spawnIndex], spawnPos, obstaclePrefab[spawnIndex].transform.rotation);
}
Comment
Answer by Llama_w_2Ls · Jul 12, 2021 at 12:35 PM
Just parent it to the background after instantiation. E.g:
var obstacle = Instantiate(obstaclePrefab[spawnIndex], spawnPos, obstaclePrefab[spawnIndex].transform.rotation);
// Re-parents it to this game object (the background)
obstacle.transform.SetParent(transform);
Your answer
Follow this Question
Related Questions
Variable being shared between clones 2 Answers
How can I destroy an specific clone already instantiated, when there's more than one? 1 Answer
Trying to Clone 1 of 5 GameObjects in a array 1 Answer
Changing a variable on an instantiated prefab clone once created 1 Answer
Sometimes one sometimes two clone is formed. I just want to form a clone 1 Answer