Checking for collision with one of my own "classes"(?)

Hello everybody,

I’d like to know how I can check if a collision in the OnCollision… Method was with a specific class.

For example…I create a new GameObject and name it “objEnemy” …and it has a rigidbody and a BoxCollider.
Now some other objPlayer get its OnCollision… triggered.
How would I check if the collision was with a instance of the type(?) “objEnemy”?

I know I can achieve this with the “Tag” field…but…that just seems liek a bad workaround…and, what about inheritance…maybe the object inherits from objEnemy (is inheritage even possible with unity).

So, yeah…I just started with unity and hope u can help me out here.
Thanks in advance

From the hit info get the gameobject than run its GetComponent() method for your class. If it returns null the component isn’t there if it returns your object you know its there

I suggest you use OnTriggerEnter but if you want to use OnCollision here is an example:

 void OnCollisionEnter(Collision Other) {
        objEnemy Temp = Other.collider.GetComponent<objEnemy>();
        if (Temp != null ) {
          //Do whatever
            }
        }

Okay…so, I would have to name my scripts “objEnemy” n whatever? …

I still have to get my head around the fact that a GameObject is just a container and the SCRIPT you might use basicly defines the class…so weird.

Thanks anyway…

But, what if I have a GameObject as a prefab that does not have any logic / script included…and I want to check for this…I couldnt, right? It has no script and thus no custom class attached after all :frowning:

If you not no script on it yes you are correct, you would have to use the tag or name of the object.

Wow…bleh…thats kinda ugly imho.
Well, this will take some time getting used to :slight_smile:

Thanks!

You could always just toss a empty class on it.

I dont understand why you find this weird, if you dont use a tag, the GameObject’s name or a class attached to the GameObject, how else did you expect to tell GameObjects apart?, what other identifying qualities an empty GameObject has?

In your opening post you said about checking the object’s name, this is a really bad idea, I tell you why if you create a GameObject and make it a prefab when you intantiate that prefab it wont have the same name as the original object it will have a suffix (the suffix “(Clone)”) added to its name.

The solution cmcpasserby and I gave you is the best alternative to using tags, you dont need to check for custom class you could check for a rigidbody (like you said your object has) for example instead of a custom class.

Ya get component works on all components including built in api ones so use GetComponent to check for the existence of a rigid body.

Well, I’d prefer if every newly created GameObject would come with a default script that is based on the GameObjects name…

But, I guess with Unitys flexiblilty and with the fact that u do not always need logic in a GameObject this would be annoying too. I will just have to get used to it.