- Home /
How to check if gameobject name is something or somethingElse
So i would need to check if an object is something or somethingElse.
function OnTriggerEnter(object : Collider)
{
if(object.name != "Test's Laser Turret1" || "Test's Laser Turret2" || "Test's Laser Turret3" || "Test's Laser Turret4" || "Test's Laser Turret5" || "Test's Laser Turret6" || "Test's Laser Turret7" || "Test's Laser Turret8")
{
Instantiate(explosion, transform.position, transform.rotation);
Destroy(gameObject);
}
}
You should definitely spend some time in the learning area of the website before investing more time experimenting.
First off, you should probably have these objects in a collection somewhere, so you can loop over the collection.
If applicable, you could give these objects a tag and check for that, to avoid the hard-coded check for unique sting names. (these are almost always a bad idea)
If you really need a string comparison, and it really needs to work just like this, you could at least clean it up a bit like so:
bool wasNotAny = true;
string str = "Test's Laser Turret";
for (int i=1; i<9; i++) if (object.name == str+i.ToString()) { wasNotAny = false; break; }
if ( wasNotAny ) { // do stuff }
Sorry for the useless question guys... I easily fixed the problem myself by using:
function OnTriggerEnter(object : Collider)
{
if(object.name != "Test's Laser Turret1")
{
if(object.name != "Test's Laser Turret2")
{
if(object.name != "Test's Laser Turret3")
{
if(object.name != "Test's Laser Turret4")
{
if(object.name != "Test's Laser Turret5")
{
if(object.name != "Test's Laser Turret6")
{
if(object.name != "Test's Laser Turret7")
{
if(object.name != "Test's Laser Turret8")
{
Instantiate(explosion, transform.position, transform.rotation);
Destroy(gameObject);
}
}
}
}
}
}
}
}
}
Ps. The idea was to create a bullet that would only explose if it hit a non friendly turret.
this is a nasty piece of code. and should you decide to add another turret you are going to have to do a lot of rewriting, as mentioned above you should definitely look at using tags ins$$anonymous$$d of names, that way you can
// set tag to "turret" in inspector for all turrets.
function OnTriggerEnter(object : Collider)
{
if(object.transform.tag == "turret")
{
Instantiate(explosion, transform.position,transform.rotation);
Destroy(gameObject);
}
//you can also check for other things at the same time.
if(object.transform.tag =="health")
{
//do some health bonus here.
}
}
always look to see if there is a way you can create reusable code, something that doesn't rely on specific names for each object, if you have to identify each turret individually, it might be worth indexing them and then using getComponent to reference the turret index.
Answer by Mathieu_Dossmann · Oct 31, 2014 at 01:49 PM
You have your answer in your question :
it is either something or somethingElse. So why not just declaring some properties in your TurretClass like :
public isFriendly = false; //it's is either friendly or not
public typeDescription = "Turret";
Then in you GameObject Class (presumably a projectile i am guessing) making the method as :
function OnTriggerEnter(object:Collider) {
if(object.isFriendly == false && object.typeDescription =="Turret")
{
//Do something with your gameObject
}
}
Answer by Wisearn · Oct 31, 2014 at 01:47 PM
The problem here is:
if(object.name != "Test's Laser Turret1" || "Test's Laser Turret2"
This is not how the operators work, you would write it like this:
if(object.name != "Test's Laser Turret1" || object.name != "Test's Laser Turret2" || object.name != "
et.c.
Even more, in this case you actually want && (AND) instead of || (OR), otherwise it will check if the object.name is not Test's Laser Turret1 and because this is true it will execute your code because it doesnt require the other ones to also be true (which is what && ("and also") would fix).
So what you really want here is:
if(object.name != "Test's Laser Turret1" && object.name != "Test's Laser Turret2" && object.name != "
But of course when you become a serious programmer and you see repetitive patterns like these what you want to do is make functionality that will handle the small difference for you or exploit what the Turrets have in common with eachother, just like @AlwaysSunny and @Mathieu_Dossmann posted.