- Home /
Script calling onCollisionEnter2D and onTriggerEnter2D while disabled
I have a unit which spawns a shield gameobject when he is hit by a bullet. Here is the code responsible for this:
public class TitanMove : AIMove
{
GameObject shield;
public GameObject shieldPrefab;
float lastShieldTime = -100;
float lastShieldBreakTime = -100;
bool shielded = false;
float shieldCooldownTimer = 0;
float shieldCooldown = 6;
.....
public void OnCollisionEnter2D(Collision2D col)
{
if(col.gameObject.tag == "bullet" && shieldCooldownTimer == 0)
{
spawnShield();
}
}
public void spawnShield()
{ //Spawns a shield prefab around the unit that blocks damage.
shielded = true;
shield = (GameObject)Instantiate (shieldPrefab);
shield.GetComponent<ShieldScript>().parentUnit = this.gameObject;
shield.GetComponent<ShieldScript>().Initialize();
lastShieldTime = Time.time;
}
}
My problem is that this script seems to be able to call these even when it is disabled. This is easily fixed by adding an if(this.enabled) check, but why can OnCollisionEnter2D and OnTriggerEnter2D be called from disabled scripts?
Comment
Answer by justoon · Jan 13, 2015 at 02:45 AM
Disabling a script means it will no longer be included in the Update loop, but the script will still respond to events.
IF you want your script to ignore events like OnCollisionEnter2D or OnTriggerEnter2D you can disable the collider(s).