- Home /
Unable to disable collider2d and renderer of gameobject.
Object A checks for collisions from Object B. If there are collisions, then the script is supposed to disable to collider2d and renderer of Object A. This is not happening currently. OnTriggerEnter2d is an IEnumerator because I need to run a Coroutine in there. Script is attached to Object A. gameobject.collider2D.enabled & renderer.enabled don't work either.
bool invulnerable = false;
IEnumerator OnTriggerEnter2D(Collider2D kaboom)
{
if (!GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerDamage>().invulnerable)
{
if (kaboom.gameObject.tag == "Crowd")
{
collider2D.enabled = false;
renderer.enabled = false;
Debug.Log("Radius is colliding with crowd");
}
}
}
I think you should make the method void and call another IEnumerator method that will start coroutine. Other than that, your code seems to be correct. However, if you want to disable renderer and collider, perhaps deactivate the GameObject would be easier.
gameObject.SetActive(false);
gameObject.SetActive(false);
Debug.Log (gameObject + "has been disabled");
This did not work either. When the collisions happen, nothing gets logged. Why is this happening ?
Debug.Log should be above gameObject.SetActive. Script will not work anymore if you set gameObject disabled.
That makes sense, though it is not getting disabled when I see it play in Unity. Commenting out the IF statement to check the Invulnerability, allowed for the gameObject.SetActive(false); to happen.
Answer by coolbird22 · Apr 28, 2014 at 04:03 PM
Something was wrong with how I had proceeded with the code. Rewrote the code with an entirely different approach and it does not make sense to post it here, as it will only end up confusing, than help.
Your answer
