dhully
1
On being Invoked, the game object needs to check if it is colliding with a different game object, which it isn’t currently. What am I doing wrong ?
public bool invulnerable = false;
void OnEnable()
{
Invoke ("CheckForCollisionsAndDestroy", 10f);
}
void CheckForCollisionsAndDestroy()
{
gameObject.SetActive (false);
IEnumerator OnTriggerEnter2D(Collider2D collision)
{
if (!invulnerable)
{
if (collision.gameObject.name == "Crowd(Clone)")
{
Debug.Log("Crowd Hit");
}
}
}
}
void OnDisable()
{
CancelInvoke ();
}
I’ve never seen collision detection implemented that way?
In a script attached to a gameobject with a collider you should have a method with the signature:
void OnCollisionEnter2D(Collision2D collision)
{
//Do your stuff
}
Seems you only want to detect collisions after some event has triggered your invulnerable variable.
So in your Start function you could do something like:
bool invulnerable = false
void Start()
{
//Start off with the collider disabled
collider2D.enabled = false;
}
And in the update function
void Update()
{
if(the condition that makes invulnerable true)
{
collider2D.enabled = true;
}
}
I’ve never used OnEnable to be honest. But I assume the same logic applies:
void OnEnable()
{
collider2D.enabled = true;
}
Assuming you want to disable collision detection on disable
void OnDisable()
{
collider2D.enabled = false;
}
That could/should do the trick, perhaps not though, it’s pretty late here haha!