Instantiating GameObjects after a certain amount has been reached?
I want to be able to instantiate a prefab (which I know how to do), only after the first one has fulfilled a set criteria. Example: spawning new tetromino after the previous one has touched the bottom of the board. Is there any particular way of doing this?
Answer by UnityCoach · Dec 10, 2016 at 08:21 PM
Hum, this is an interesting case of project architecture.
One may prefer to have a "controller" that controls the object, and therefore knows when it reaches the ground, and can trigger the instantiation.
You may also use events, it's pretty handy when you don't know when something is going to happen, and especially which object is going to raise the event.
In your case, I understand only one object (the spawner) must be notified of the event, so you may just have your Spawner "watch" the object position. Like
private Transform current;
public GameObject reference;
public Transform limit;
void Start ()
{
Spawn();
}
void Update ()
{
if (current.position.y <= limit.position.y)
{
Destroy(current.gameObject); // destroy current
Spawn();
}
}
void Spawn ()
{
current = ((GameObject) Instantiate (reference, transform.position, transform.rotation)).transform;
}
Answer by Velocitys · Dec 10, 2016 at 12:40 PM
Hey i saw you're post
What do you mean with the bottom of the board ? What you could do if i think what you mean with it. You can make a collider on the bottom of the board and when the collider is triggered. You instantiate a new object :) I am developing my program skills with helping people. Correct me if i make some mistakes.
Its a good Idea, but the one problem is that the game is 2d and colliders do not work the same, I have to use 2d collision, and funny thing, I tried it already, the problem is that when it touches, it continuously spawns them, even when not using OnCollisionStay. Also, I need to check if all of the game objects are touching the bottom of the board (the tetris board, where all the tetro$$anonymous$$os are held when they are instantiated and start falling on each other) before I instantiate a new one, for example, at the start of the game there would only be one. Once that one hits the bottom and stops, the next one would instantiate, and then when that one hits the bottom or the other one, the next one would be instantiated, because it would have checked that both were touching rather that just one. $$anonymous$$ake sense?
Yes i get what you are trying too say. If you havent figured it out by tonight i can help. I am not home right now. But i maybe have a solution you can use a raycast on the blocks when the blocks are in lets say a range of 1.0f then you can instantiate the new object.
Your answer
![](https://koobas.hobune.stream/wayback/20220612102411im_/https://answers.unity.com/themes/thub/images/avi.jpg)