F
F
FrOms2021-05-25 10:11:21
C++ / C#
FrOms, 2021-05-25 10:11:21

How to remove an object when it passes through a certain point?

I have a Map object on which all the scripts are dressed and the Ground, Obstacle objects are placed inside it, it also moves backwards relative to the Z axis and when it reaches a certain point, clones of the Map object are created. I need to delete the Map object if it reaches the -100 point. But when he reaches it, his clones are also deleted, and I need each one to be deleted in turn.
Here is the code:
GroundMovement Script:

if (map.position.z <= 0 && !spawned)
        {
            FindObjectOfType<GroundSpawner>().SpawnWave();
            spawned = true;
        }

        if (map.position.z <= -100)
        {
            FindObjectOfType<GroundSpawner>().DeleteWave();
        }

Ground Spawner Script:
public void SpawnWave()
    {
        Instantiate(objToSpawn, map.position + offset, Quaternion.identity);
    }

    public void DeleteWave()
    {
        Destroy(objToDelete);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pashara, 2021-05-25
@pashara

Does the trigger actually work?

Note: Both GameObjects must contain a Collider component. One must have Collider.isTrigger enabled, and contain a Rigidbody . If both GameObjects have Collider.isTrigger enabled, no collision happens. The same applies when both GameObjects do not have a Rigidbody component.

The deletion logic does not work, at least due to the fact that the event of entry into the trigger area is not caught.
PS Source https://docs.unity3d.com/ScriptReference/Collider....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question