- Home /
DisablingOnCollisionEnter function
Hello,
I have a very strange problem. I have a script to manage all the collisions of the character, so its only function is OnCollisionEnter. It works fine but when I'm trying to disable it, using
Player2.GetComponent(Collisions2).enabled = false;
Player1.GetComponent(Player_Sidescroll).enabled = false;
It appears as if it disables (the checkbox in the inspector unchecks itself). but it still works! The other script disabled works fine... Weirder still, even if I disable the code in the inspector it still works. Only if I delete it, then it stops working. Again, the only function on the collision code is
function OnCollisionEnter(hit : Collision)
{
if(hit.gameObject.tag == "fireball")
{
Destroy(hit.gameObject);
//Generate Explosion
var explosion = Instantiate(ExplosionPrefab,transform.position, transform.rotation);
Health2.damage += 103; // do damage
playerSprite.DoAnim("Hurt"); // animate
}
}
I have no idea why it does this. Any ideas?
Answer by SilverTabby · Jul 04, 2011 at 11:32 PM
Add a new variable
var isCollisionFunctionEnabled : boolean = true;
then, at the start of OnCollisionEnter function, add this:
if(!isCollisionFunctionEnabled)
return;
This will make it so that the function will no longer perform it's calculations once the variable is set to false. However, it will not prevent the function from being called.
Thanks this is a working solution. However I still think that's weird. A bug?
Answer by Molix · Jul 05, 2011 at 02:45 AM
The boolean in the function can work if you need the component or other components to continue working for something else, otherwise you can deactivate the GameObject, and that should get rid of the collisions,
i.e. gameObject.active = false;
Your answer
Follow this Question
Related Questions
On Trigger Enter, Collide with object, specific collision 1 Answer
Simple Collision Detection not working. [Solved] 3 Answers
disable collision on an object when pressing Q (3D) 1 Answer
Collision Only being detected on one of the objects involved in the collision - C# 0 Answers
How do Unity manages collisions in a game internally? 0 Answers