Check if Object is itself. C#

is there anyone out there who knows how i can check if the colliding object is object?
what i accualy mean is, is there something to do the following:

public Transform SomeOBject;
void OnCollidionEnter (Collision other){
if(other==SomeObject){
print(“other is SomeObject”);
}
}

is there anything that does this? cause what i just typed is not possible, and gives shitloads of errors.
many thanks to the people who awnser!

I can probably give you the answer you're looking for, but first I need to know: How is the other object created? How are you keeping track of it? (Which might be the whole issue - if you're not) Is the "SomeObject" a "type" or a specific instance of an object?

3 Answers

3

Collision is not the same as Transform; they are two different types so they can’t be compared with operator ==. You can however compare two of the same type are identical.

All MonoBehaviour have access to these objects, you just need access to them by their identifiers: transform, Unity - Scripting API: Component.collider, rigidbody, and gameObject.

From here, you can see that Collision has information stored about the object it berlongs to. Namelt, it has a rigidbody, a transform, a gameObject, and a collider references.

Take your pick of which one you want to use.

Just a shot in the dark, try:

if(other.transform == SomeObject)

instead of

if(other==SomeObject)

public Transform SomeObject;
 void OnCollidionEnter (Collision other)
 {
     if(other.transform == SomeObject)
     { 
         print("other is SomeObject"); 
     }
 }

tried, but without succes D:

That's odd - this is how I'd do it. You're not accidentally checking against a prefab are you?

not where i'm aware of. the SomeObject that i'm trying to compare is found by this Transform nextWaypoint = transform.Find("/" + WaypointBox + "/Lvl_Test_Waypoint_" + WaypointBox + "_" + TenWayNum + OneWayNum); and the "other" is set by the trigger it in. and i dont get a Null when i print the transform.find.

And they're directly connected - the collider is on the way point? And you have spelled it OnCollisionEnter not OnCollidonEnter as it appears in your question and this answer?

yes the collider is in on the waypoint, and i'm using OnTriggerEnter, and yes the collider is checked as trigger.

Well, if it’s the same type of object and there are multiple of them, you should give each object the same tag.

Then in your OnCollisionEnter function will be something like this:

void OnCollisionEnter( Collision other ) {
   if(other.tag == 'tag goes here'){
       print(other is other);
   }

It’s also possible to use other.gameObject.name in place of the tag

i'm already using tags right now, but i got multiple objcets with this tag, cause they are the same. but it needs to check for 1 specific object.