Detecting scene objects

Is there anyway I can have an object that detects other objects without using raycast or colliders? For example, you have a box that moves along a set path, and there is a sphere 500m away from it, the box can pick up that the sphere is there and run a command to destroy the game object.

Thank you in advance for any help!

Hello!
You have to be a little more precise.What gameobject is going to be destroyed?The Box?The Sphere?
Do you want the box to be able to destroy the gameobject if it is within a certain range from the sphere or if it just exists somewhere in the scene?

If you are looking for distance checking,you can find it here:

I agree with Ted that you should check out the Distance method.

Say you want the cube to be an object that lives throughout the level, and when it gets close to a specific sphere the sphere gets deleted; In your cube’s Update function you could use GameObject.Find() to find the sphere by name, or GameObject.FindWithTag() to find it by tag (you could also use FindGameObjectsWithTag() to check many sphere’s, but this could turn out to be taxing if you have tons of them in your game), then use the sphere’s position and the cube’s position with the Distance method, and if that distance returns lower than the number you want (500m), then have the cube either Destroy() the sphere or send a message for the sphere to destroy itself.

Cube JS:

var range : float = 500; //distance at which to destroy the sphere

function Update(){
     var sphere = GameObject.Find("Sphere");
     var dist = Vector3.Distance(transform.position,sphere.transform.position);
     if(dist < range){
          Destroy(sphere);
     }
}

Instead of looking for the object in update and also checking distance in update, you can create a trigger system, using a sphere collider and using OnTriggerEnter method.

Koyima that’s what i’m trying to avoid

The problem with the ‘distance’ solution is that you will not be able to find out where and how they collide, so it’s only usefull for really rough detection. It’s perfect to determine when a ship flying towards a sun should be destroyed by overheating, or if the object your talking about is a navigational icon of sorts that is entering an atmosphere or entering a black hole, or whatever scenario you could think of that would not require exact collision checks.

Another problem is that they do not actually detect your collision, or return what object your colliding with unless you parse through an array of all objects it can collide with every time.

Why?

If I decide to have any scene objects spawn in that show up inside of the trigger, then it doesn’t detect them (that’s from personal experience), also off of shigidamark’s idea can I have my all scene objects checked through and then checked for tags if I want it destroyed example:
I want it to check for all items regardless of their names, and then determine their tag, I know how to find tags (gameObject.tag).

You mean something like

GameObject[] Result = GameObject.FindObjectsOfType(typeof(MonoBehaviour))

to get the objects and then check their distance to this object?

Should work, as long as you don’t expect large spacebattles, asteroid fields, schools of fish or other groups with lots and lots of objects…expect low FPS if you do…

Thanks, no more than 10 objects at a time!
That sorts out my questions, Merry Christmas!