How to Spawn a Prefab at an already Spawned Prefab's that's from an array at their transform.position?
I'm trying to spawn prefabs on to other prefabs that have already been spawned from an array.
So, let's say I spawned 10 prefabs from my array.
I now need to spawn a prefab at each and every one of the previously spawned prefabs position. This need to be done one at at a time.
How do I go about doing this?
I'm thinking that getting each spawned elements transform.position would be one way to go about it, but I don't know how to get the transform.position for each spawned prefab's respective element.
Maybe another way is to name each clone uniquely myPrefab1, myPrefab2, etc, etc. and then spawn the second prefab on top of them that way, but again, I do not know how to get each prefabs transform.position.
Here's my code to give you an idea of what I'm trying to do:
if (Input.GetMouseButtonUp(0)){
if (i<whatEverYouFeelLike){
stuffFromArray = Instantiate(stuffArray[i], spawnPosition, transform.rotation) as GameObject;
stuffFromArray.name = "stuffFromArray " +i.ToString();
i++;
}
}
if (Input.GetMouseButtonUp(0) && spawnSecondRound){
// Spawn Second Round of Stuff Here
// Spawn Second Round of Stuff at the stuffFromArray.transform.position, separately and one at a time, each time the mouse button is clicked
// How?
}
Any help is really appreciated.
Thanks!
GameObject myPrefabSphere = Instantiate (prefabSphere, stuffFromArray[i].transform.position, transform.rotation) as GameObject;
I'm trying to get
stuffFromArray[i].transform.position
to move on to the second i in the array to spawn another sphere.
Not sure how to do that.
Answer by Mr_Edward · Dec 10, 2015 at 08:59 AM
I'm not quite sure I completely understand what it is that you wan't to do.
Anyway: You can get the first prefabs transform quite simply by calling
stuffFromArray = Instantiate(stuffArray[i], spawnPosition, transform.rotation) as GameObject;
debug.log("Prefab position: " + stuffFromArray.transform.ToString());
I'll modify your script to do what I think you want it to do, but then again I'm not quite sure I understood you correctly:
if (Input.GetMouseButtonUp(0))
{
//(I just added these so I didn't get any compiler errors)
//---------------------------------------------------------------------------------
int i = 10;
int whatEverYouFeelLike = 1;
GameObject[] stuffArray = new GameObject[5];
GameObject[] SecondSpawnstuffArray = new GameObject[5];
Vector3 spawnPosition = transform.position;
//---------------------------------------------------------------------------------
if (i < whatEverYouFeelLike)
{
//You don't have to save "stuffFromArray" for any longer than this frame, and you only use it in this if statement. Therefor you can define it here, by putting "Gameobject" in front of it.
GameObject stuffFromArray = Instantiate(stuffArray[i], spawnPosition, transform.rotation) as GameObject;
stuffFromArray.name = "stuffFromArray " + i.ToString();
GameObject SecondSpawnStuffArray = Instantiate(SecondSpawnstuffArray[i], stuffFromArray.transform.position, transform.rotation) as GameObject;
SecondSpawnStuffArray.name = "SecondSpawnstuffFromArray " + i.ToString();
i++;
}
}
//You don't actually need this, atleast not for what you have presented on the forum. If you need it for some other reason, you can simply save the transform to a private variable, and use it here.
/*
if (Input.GetMouseButtonUp(0) && spawnSecondRound)
{
// Spawn Second Round of Stuff Here
// Spawn Second Round of Stuff at the stuffFromArray.transform.position, separately and one at a time, each time the mouse button is clicked
// How?
}*/
So, what I'm trying to do is spawn some prefabs from an array for the example lets just say they're squares.
Every time the mouse button is clicked a new square spawned from the array is moving. Let's say there's 10 of them for the example.
Now, the part where I'm stuck is I need to spawn a sphere above each of the already spawned squares.
Each time the mouse button is clicked a sphere is to spawn above each of the squares, 1 sphere at a time, each time the mouse button is clicked.
Preferably this needs to be done in the same sequence as the order from which the squares were spawned but this isn't totally necessary.
Here's some code that might help to understand better:
GameObject myPrefabSphere = Instantiate (prefabSphere, stuffFromArray.transform.position, transform.rotation) as GameObject;
That code will spawn the spheres all at once when the mouse is clicked. I need for the spheres to spawn only one at a time.
Hope that helps to clarify.