comparing strings|easy way.....

Hi, i have a compare tag statement in my OnTriggerEnter… it look like this:

if(otherTag == “Enemy” || otherTag == “EnemyWall” || otherTag == “EnemyUnion” || otherTag == “EnemyWall2”)

but i want to simplify it like this:

if(other.tag == “Enemy”||“EnemyWall”||“EnemyUnion”||“EnemyWall2”)

but it dont work…theres any reason why its not working?? or any other way to simplify it??

thanks.

2 Answers

2

ok i solve it by myself.
i just entered every string in a “()” heres how:
//from…
if(other.tag == “Enemy”||“EnemyWall”||“EnemyUnion”||“EnemyWall2”)
//to…
if(other.tag == “Enemy”||“EnemyWall”||“EnemyUnion”||“EnemyWall2”)

No, this definitely won’t work: the operator || means logical OR and only accepts boolean operands. You’re already doing this the right way: get the tag in a string variable and compare it to each string - this allocates a string in memory only once per OnTrigger event. An alternative that doesn’t allocate a single byte is CompareTag:

function OnTriggerEnter(other: Collider){
  if (other.CompareTag("Enemy") || other.CompareTag("EnemyWall") || other.CompareTag("EnemyUnion") || other.CompareTag("EnemyWall2")){
    // do something
  }
}

You must write a lot more, but apparently the performance is a little better.