So Vector3.Distance needs two objects; a target and itself, at least that’s what I am doing. The thing is that I have to explicity state what the target gameobject is i.e. making a public variable and dragging and dropping through the inspector, or access some sort of list/array of these objects (which would mean I need to populate these before hand).
I wanted to know whether it is possible to have an object constantly checking the distance between itself and any other object in its surroundings without explicitly passing it off as a parameter?
If you tag all the relevant objects you could probably use GameObject.FindGameObjectsWithTag(“spawn”);
And you could probably check the list with an InvokeRepeating(“method”, 2.0f, 0.3f) for fewer updates than the Update function.
If the gameobjects are instantiated you could place them in an object pool instead if finding their tag?
…or sphere colliders could also work for fixed distances?
I think you need to separate the process in two steps.
first step marks all object that are within distance with a flag of some sort, a boolean? At their bullettime position.
Second step execute what to be done to all objects that has been flagged (or what to do with a calculated mean value).
A boolean can be set to true many times and thus limit your possible errors. Then you loop through your list of objects and check what to do with them that has been flagged. Think of a dynamic system of particles. First you have to make an evaluation, and only later compute a weighted result.
So it’s not an unknown object - it’s each object within some distance from the given object. So the real question you’re asking is “How can I get all the objects within X distance of my object”.
You could brute force it by checking every object in the scene. That’s easy but slow and wasteful as more things are present.
You could use Find or FindWithTag etc but…don’t.
You could use FindObjectOfType if all the things you want to check have a common and unique component. IE if they all have an Enemy component attached you could do FindObjectsOfType() but again this can be slow.
Your Enemy (or whatever) could keep a static list of all alive instances that you add to in Awake or OnEnable and remove from in OnDestroy or OnDisable
If the things you want to check have colliders on them you could use Physics.OverlapSphere to get only nearby things
You could add a sphere collider and set it to be a trigger to your object and then use OnTriggerEnter/Exit to maintain a list of nearby things