Problem with triggers and continuous spawning of GameObjects

Hello everyone, I’m new to Unity and I’m trying to build a 2D game. What I’m trying to accomplish is having a continuous spawning of GameObjects one after the other. I tried by using the Invoke function with a variable delay but when I change the speed of the GameObjects the invoke function seems to not work so well (even though I change the delay accordingly). So I tried to create an empty GameObject called Respawn with a BoxCollider2D (and with the isTrigger property checked) that every time a RigidBody passes through it should call the function Spawn(), but it doesn’t.
Here is what I did: I attached to Respawn a script with the function OnTriggerEnter(Collider other) (which, for some reason, is never called) and I’m sure that the GameObjects that should trigger Respawn haven’t the isTrigger property checked and have both a RigidBody2D and the BoxCollider2D. The problem is that these GameObjects are able to trigger the OnCollisionEnter2D() function with another empty GameObject that I use to destroy those GameObjects, but they don’t trigger Trigger. Do you have any ideas?

Is “haven’t” a typo, or is the collider’s “isTrigger” property actually not enabled?
“isTrigger” is required to be checked for the collider to receive OnTrigger... events.

Hello, thank you for your answer. I solved my problem in a completely different way, but if someone needs to do something like this in the future, I want to understand what I was doing wrong. So, in the Unity documentation is said that if both the receiver of the trigger (in my case “Trigger”) and the GameObject that triggers “Trigger” have isTrigger checked then no collision happens. That’s why I’ve only checked the Trigger’s isTrigger property, and not the GameObject’s one. Thanks for helping

GameObjects don’t have a IsTrigger property. Being a trigger is a behaviour of a Collider only.

When two colliders meet, the contact is dealt with as a trigger contact (no collision response) if either of the colliders are a trigger. If neither are then it’s dealt with as a collision (with a collision response).

I’m glad you got your project working though.

I’m sorry, I used the term “GameObject” incorrectly. In order to clarify, what I have is actually a Prefab with attached a BoxCollider2D and a RigidBody2D and when it hits what I called “Trigger” nothing happens. In my case, the Prefab doesn’t have the isTrigger property checked, but “Trigger” does. And the object that has the script attached that should do something, of course, is “Trigger”. I hope I was clear

If by “nothing” you mean they don’t collide (no collision response) but you get OnTrigger callbacks then that is correct. You don’t need both to be so in your case the “prefab” doesn’t need to be. Only one of the colliders in a contact needs to be.

If you yourself could have objects pass through you without interaction then you wouldn’t need those others object being thrown through you to have any special abilities like yours. Same here.

Unfortunately, by “nothing happens” I mean that the OnTriggerEnter() method is never called

Well you only need one of the colliders to be a trigger to get that callback, not both so I cannot tell you what’s going wrong there but it won’t be a bug because that would be astoundingly bad and the 2D physics would be on fire right now with bug reports. :slight_smile:

You put “OnTriggerEnter” but I hope you mean “OnTriggerEnter2D”.

EDIT: More concerningly I saw you put above “OnTriggerEnter(Collider other)” but that’s for 3D physics.

ohhhh, maybe that’s the problem!