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?