DisablingOnCollisionEnter function


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")
		//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?

Add a new variable

var isCollisionFunctionEnabled : boolean = true;

then, at the start of OnCollisionEnter function, add this:


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.

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;