Problem with SetActive and OnTriggerEnter2D

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.

2791198--202387--problemTrigger1.png 2791198--202388--problemTrigger2.png

Thanks for your help

Hi.

How do you call StartWater() method?

Hey,

I call it from another class and it is called cause when I put a break point in the FixedUpdate() of the effector, it stops.

However, I found why it was not working, I had an entity with the tag Player and on the layer Player which spawn another entity. And I put this new entity on the same tag AND the same layer. It seems that it doesn’t work when 2 controllable gameObjects are on the same layer. I don’t really understand if it’s because I use a specific script to control the player’s movement (and it would not support 2 controllable gameObject on the same layer). But when I change the layer on the spawned one, the collider is working again.

Thanks for your answer

Isn’t it the case of Layer Collision Matrix? Maybe the collision you want to happen is ignored? (Edit>Project Settings>Physics 2D)

I don’t think I quite understood what is going on with that player and spawned entity - could you provide us with more screenshots and preferably add some descriptions to them, it would help a lot.

I checked the matrix but everything was checked (I unchecked some after to save some process work tho ^^).

I’ll add some screenshots when I’ll be on my computer.

In some cases “Everything checked” causes the problem. For example you expect OnTriggerEnter2D to happen between A and B objects, but some other random object C was blocking it. But anyway that’s not what causes the problem because OnTriggerEnter2D is not called at all.

At this point I suspect that GeyserEffector gameObject is not properly referenced in WaterZone’s Effector. For testing purposes make sure you drag it from the scene.