either...or IF statements

Need to get the either…or if statement to work. By this I mean if condition A or B is met. According the ref guide, the " | " symbol is used (ex. if (x == 1 | 2 | 3). I want to use it for three separate objects : “targ1” “targ2” “targ3”. I haven’t found the correct syntax to do this for object names or tags.

function OnCollisionEnter(shotCollide : Collision)	
{
	
	if (shotCollide.gameObject.name ==("targ1" | "targ2" | "targ3"))   //this syntax is wrong
	{
				
		
	        if(!shotCollide.gameObject.rigidbody)
		{
			shotCollide.gameObject.AddComponent(Rigidbody);			
			
		}
	}
}

It’s || (double pipe), as in:

if (shotCollide.gameObject.name == "targ1" || shotCollide.gameObject.name == "targ2" || shotCollide.gameObject.name == "targ3")

alternatively… (if you like filthy hacks)

if("targ1targ2targ3".Contains(gameObject.name))
{

}

Some options:

var name = "targ1";


//All of these return true for targ1, targ2 and targ3... but vary in *how* strict they are and how flexible.
Console.WriteLine("targ1targ2targ3".Contains(name));  //Warning, this returns true for 'g1tar' etc.
Console.WriteLine(new[] { "targ1", "targ2", "targ3" }.Contains(name));
Console.WriteLine(name == "targ1" || name == "targ2" || name == "targ3");
int temp;
Console.WriteLine(name.StartsWith("targ")  //any suffix is fine
                   int.TryParse(name.Remove(0, 4), out temp)  //any integer suffix
                   temp > 0  temp < 4);  //within a certain range.
Console.ReadLine();

Interesting. Never thought of this.

There’s a good reason he’s calling it a “filthy hack”, though.

I prefer linq lol

string[] stringArray = { "targ1", "targ2", "targ3", "targ4" };
if(stringArray.All(s => shotCollide.gameObject.name.Contains(s))){
//Do something
}

thanks for all the answers; i considered using an array but decided otherwise

Note that if you are using UnityScript or Boo, you can use the ‘in’ keyword.

Like Contains() but with nicer syntax. Boo Example below:

if shotCollide.gameObject in ["targ1", "targ2", "targ3"]:
    DoSomething()

Don’t you mean ‘Any’?

Since I’m a C# fanatic, I’d suggest doing something like the following:

if (shotCollide.gameObject.name.In("targ1", "targ2", "targ3")
    DoSomething();

....


public static class Helper
{
    public static bool In(this string element, params string[] collection)
    {
        return collection.Contains(element);
    }
}