- Home /
Confused using Arrays
Hi, I'm using an Physics.OverlapSphere to check an area for enemy, I'v got a string array with some enemy to look for as below -
var enemyName : String[] = ["Terrorist","Sentry","DroidBox","DroidCylinder","DroidSphere"];
So I thought by checking like below it would go through the array until it finds a match then //Do Stuff, but I was wrong again, why? -
if(col.name.Contains(enemyName[i])){
Answer by Eric5h5 · Feb 17, 2013 at 08:27 AM
Contains is used to check if an array contains a particular item, so what you have is backwards. It should be
if (enemyName.Contains(col.name)) {
Hi Eric, thanks for the input, but the Contains in my original code is looking for a part of enemy name, I have Sentry1, Sentry2, Sentry3 .. ect so in the OverlapSphere loop it checks for part of that name -
if(col.name.Contains(Sentry)){
So in that I'm not using Contains to check an array but to check for part of the collider.name
It sounds like you should be using tags ins$$anonymous$$d, then you could just have a "Sentry" tag, so you wouldn't need to bother with any of this.
I don't see how that would help Eric, as I'm trying to see what is in the OverlapSphere radius and act upon finding any of the enemy Terrorist, Sentry, DriodBox .. ect so if they where tagged as so wouldn I still need to check in the loop to see if anymore are there -
if(col.tag == " Sentry"){
So I would still not need to loop through all the colliders to see if anymore are there as -
if(col.tag == enemyTag[i]){
It would help because you wouldn't need to do stuff like try to find parts of a name, you'd just use a tag. It would be more straightforward, and is why tags exist.