- Home /
OnCollisionEnter doesnt work when instantiated
Hi,
I have this strange problem with collision detection. The "OnCollisionEnter" works fine if it's attached to a gameobject which I put into the Scene manually; however if I want to instantiate the same gameobject's prefab (example: by pressing a button) the gameobject prefab is instantiated, but the collision doesn't work anymore.
WTH? Help please p.s. I have tryed the OnCollisionStay, but it didnt help :(.
The code (enemy):
var BoomExplosion : GameObject;
function OnCollisionEnter (TriggerEnter : Collision) { if(TriggerEnter.gameObject.name == "BoomExplosion(Clone)" || TriggerEnter.gameObject.name == "BoomExplosion") {
Instantiate(SpawnExplosion, gameObject.transform.position, gameObject.transform.rotation); Destroy(gameObject);
} }
It's not very helpful, but I have this exact same problem. And previously I was instantiating the prefabs and it worked fine.
I can't see where I've changed anything, and on inspection the prefab I put in the scene manually is absolutely identical to the one I instantiated.
I feel the need to bump this question, as I am faced with the same issue. I have a little side project (not a game, but a fancy 3D program) where I depend a lot on instantiation. I need to check for collisions between these instantiated objects.
An OnCollisionEnter function with nothing but a debug statement proves it's not working. All objects carrying a collider also have a rigidbody attached, as I am moving them at runtime.
They are colliding, 100% sure (the second "Click to edit" text object is instantiated during runtime):
Answer by nyanko · Jul 04, 2014 at 03:32 AM
Why don't you judged by GameObject's tag name?
var BoomExplosion : GameObject;
function OnCollisionEnter(TriggerEnter : Collision)
{
if (TriggerEnter.gameObject.tag == "your tag name") {
// ...
}
}
Your answer
Follow this Question
Related Questions
How can I keep track of score without each new basketball shot reseting my points? 1 Answer
Collision between two instances of the same object 1 Answer
OnCollisionEnter not triggering when two rigidbody collide via Instantiate 1 Answer
How can I keep track of score without each new basketball shot reseting the points? 0 Answers
I don't think I get collision box, can someone help? 0 Answers