Simple tag doubt.

I want to know when any object of the game with the tag “enemy_character” is 20 near to the player, so i try to use a Vector3.Distance:
if(Vector3.Distance(this.transform.position, .gameObject.tag == “enemy_character”) < 20)
I think that I need something in front of .gameObject.tag, or is there another way to put "Any object with the tag “enemy_character”?

Thanks

Far as I know, you’ll have to create a collection of enemies and then in an update, coroutine, or other loop, iterate through this collection checking to see if any are close to you.

You could also possibly just have a trigger collider around your player and trigger an action when an enemy enters into the collider or exits it.

It looks like you’re trying to get the distance without knowing what you’re getting distance to.

You have to find all of the GameObjects nearby probably with an overlap sphere, then loop through them to compare their distance. There are lots and lots of tips online for doing this.

Yes, i used this →
if(Vector3.Distance(this.transform.position, .gameObject.tag == “enemy_character”) < 20)
inside of the update. but what have i put in the second place :
if(Vector3.Distance(this.transform.position, —HERE— ) < 20)
to say every object with the tag enemy_character, gameObject.tag ==“enemy_character” doest work, how can i put it?

So, can´t you say in code: Every object in the scene with a “x” tag?

You probably could in one way or another, but doing so is the completely wrong way to approach your situation. There is no quick list that has all objects with “x” tag so you would have to look at all scene objects which is a complete waste. Tag is simply a string that every GameObject has on it, and you can query against it to do unique things based on what that string is.

The normal approach would be to gather all nearby objects on the layer that you are looking for into an array, then loop through the array checking the tag until it matches and you can check the distance.

Either that or keep a list of the things that would be on that tag, then you can just query that list instead having to find it with a sphere overlap.

1 Like

As @LaneFox says, no, there is no built in list. Which is what I was trying to tell you. You have to generate the list yourself and then loop through it and check the distance, or just have a trigger collider around your player where it triggers when an enemy enters.

Or you have a gamemanager script that adds enemies to a list as they are created or whatever and loop through that list to check the distance.

Basically you have to check each enemy one at a time in a loop.

1 Like

Ok thanks you, I think ill try to gather all nearby objects as you said.

Ok, ok thanks you :3