0
How to delay my code in Unity until collision is detected? (Using C#)
Let's say I have "n" number of gameobjects and I want to initiate them one by one in such a manner that the next object won't instantiate until the previous object hits the ground. Is there any suggestion on how to do that?
4 Answers
+ 3
use a collision event
void OnCollisionEnter (Collision Col){
If(count<n){
if col is gameobject.findbytag("ground") {
instantiate;
count++;
}
}
}
psuedo code, but this is how I do it. give the ground a proper tag and if collision with ground tag, run some code
+ 2
See CollisionEnter (as Forge Brain stated) and my most recent answer to your previous question.
Collision method:
https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html
Previous answer:
https://www.sololearn.com/Discuss/1228407/?ref=app
+ 2
1604064_Sharif
* Instantiate the first Object
* Use the method I showed in my previous answer
* Using either void Start() or the transform on the prefab to set the spawn location of the object(s). Make sure it doesn't touch anything right when it spawns.
That should do it.
With a rigidbody the object should fall automatically due to gravity.
0
Forge Brain, with this code, the problem seems to be that I cannot instantiate as many as I want while each object waits for the previous one to collide with ground.
If I'm not mistaken, this code instantiates a game object only if a certain pre-instantiated object collides with the ground.
But what I want is that...
First object gets instantiated by default and falls on the ground.
As soon as it touches the ground, the second object gets instantiated and starts falling. When this one falls, the next one gets instantiated and starts falling.
This keeps happening until n number of objects are instantiated.