I need to destroy a GameObject I’ve instantiated from a prefab. But I can’t use “tag” to destroy it since it’s tag is being used by other objects too. For example;
Think about 2 cubes side by side, I shoot 1 of them and it gets destroyed. There is no problem till this point. I can destroy the first object with “OnCollisionEnter” check. But the second object is the problem, because I want to destroy it at the same time the 1st one destroyed. The only unique thing I have about the second object is it’s Vector3 coordinates.
// Destroy an object at a location
void DestroyAtPosition (Vector3 location)
{
// Pick some small search radius for your own unique situation
float radius = 0.1f;
// To guarantee results, you'll want to assign a unique Layer for the
// cubes, and then do this with that layermask as a parameter
// Get the cube sitting at our location
Collider[] hitColliders = Physics.OverlapSphere (location, radius);
// You can use a for loop more easily, but this will follow with
// the given unity example api
int i = 0;
// Destroy everything in this list
while (i < hitColliders.Length) {
Destroy (hitColliders *.gameObject);*