Hey guys,
I have a BoxCollider2D which is working great when it is in my scene and active since the start. But when I want to instanciate it in my script or just set it inactive in the Awake() and put it active after that (before my other object is entering it), the OnTriggerEnter2D is not triggering anymore…
If I comment the SetActive(false) it works but I want it to be disable at start.
The monobehaviour which manage the effector :
[RequireComponent(typeof(Collider2D))]
public class WaterZoneBehaviour : MonoBehaviour {
public WaterType type;
public WaterEffector effector;
public float height;
public float width;
public float density;
public float timer;
void Awake() {
// If I let this, it doesn't trigger. If I comment it, it works.
effector.gameObject.SetActive(false);
}
public void StartWater() {
// I set it active before entering the effector collider.
effector.gameObject.SetActive(true);
effector.GetComponentInChildren<ParticleSystem>().Play();
}
}
The effector :
public class WaterEffector : MonoBehaviour {
...
void OnTriggerEnter2D(Collider2D other) {
Debug.Log("WaterEffector - OnTriggerEnter2D: " + other.gameObject.name);
// this part is not called even if my effector is set back to active. Only called when it's active from the start.
}
void FixedUpdate() {
// This part is called when I use SetActive(true) even if the OnTriggerEnter2D is not called.
}
void OnTriggerExit2D(Collider2D other) {
// same as trigger enter.
}
I use layers and I thought, maybe it was due to the fact that the effector has a different layer than his parent. But even If I put it elsewhere, it doesn’t work.
Thanks for your help