- Home /
instantiate prefabs y+1 from last instintiated prefab
I've got 15 prefabs. and i would like them to generate randomly above each other, lets say every 3 seconds, I've got this script, but it generates one on top another, i need to generate a prefab above the last prefab, like .y+1
public GameObject[] obj;
// Use this for initialization
void Update () {
}
void Switch(){
Instantiate (obj [Random.Range(0, obj.Length)], transform.position, Quaternion.identity);
}
}
Answer by POiD · Apr 02, 2015 at 03:23 PM
//drag your prefabs into this public array from the inspector
public GameObject[] myPrefabs;
IEnumerator generatePrefabs()
{
int currentY = 0;
while (currentY < myPrefabs.Length)
{
Instantiate(myPrefabs[currentY],new Vector3(<your X value>,<your Y Value> + currentY,<your Z Value>),Quaternion.identity);
currentY++;
yield return new WaitForSeconds(3f);
}
}
In some function .. that triggers these to start generating, add the following line:
StartCoroutine("generatePrefabs");
If you want the prefabs to be generated in a random order, then you'll have to add some more logic to 'tick them off' as selected. Thus an array the same size as your myPrefabs array, that just holds a 'used' or 'not used' value. Generate a random number, if it's not used, instantiate it and mark as used. If used, generate another number until you have an unused prefab spot.
the while loop will stop the instantiate after the number of my prefabs, Its a platformer, supposed to go till the player dies and not run out of levels, also cant use waitforseconds since this might cause gaps between prefabs making the game not playable because the player cant reach
Understood. You can easily change the while loop to ins$$anonymous$$d be "while (true)", but that is likely not what you want anyhow.
Are you moving the Camera or are you moving the scenery? That really deter$$anonymous$$es the algorithm you use for instantiating (and cleaning up) your prefabs.
If moving the scenery (i.e. scrolling it from just above the camera window to below), then you can instantiate at the same point just out of scene and destroy when below the camera window. Thus your Y coord for instantiating stays the same. You just have a timer to decide when to spawn the next one. Also means you can use an Object Pool to re-use the same objects.
If moving the camera, then you'd need to get the Y position of your main Camera. Then instantiate the next prefab at (Camera Y position [transform.position.y] + height of camera/2), to create the prefab at the top of the screen or just outside of it. Again a timer can be used to decide how much of a gap before you create the next one.
All depends on the design of your game.
$$anonymous$$y camera has a smooth follow attached to my character which moves jumps up on platforms, I have a quad with trigger below the camera to destroy objects when going up. i can't pool object because my prefabs are like in jetpack, where the prefabs are built from a few platforms together at different positions, like "$$anonymous$$i levels", and they all different sizes. basically what i need is to find the y of the last spawned prefab and next spawn y+2 from that object, like bounds.extants.y but im not sure what i can put as a component to measure the y, a trigger falls off the table because it screws up the physics in my game (OnTriggerEnter on my player)
Why not just store the Y of the last prefab you created? Store it in the class you are creating the prefabs in, or as a static variable if you really need to?
I'm afraid I can't visualize how you've designed your game, so my only other potential suggestion for your trigger idea, is to add an empty child object to your player and have a collider on that object that you set as a trigger. Wouldn't affect the player physics as that'll be on the collider / rigidbody on the parent (the player).
what point will it choose as the y? bottom top or middle, each prefab is different height, can i ask for the lowest y and highest y, then subtract high to low, and then add the number ive got +2 to instantiate next one?
Your answer
Follow this Question
Related Questions
Instatiating a prefab in a random position 0 Answers
Random array issue C# 2 Answers
What the heck is wrong with this script?! 1 Answer
Creating Prefabs at runtime? 1 Answer