How to use other objects instead of gameObject in onCollisionEnter()?


This script file belongs to an empty game object, having nothing but the script. no collider nor rigidbody, etc. I wanted to manage the scene with one script, that’s why I created public references and assigned them in the inspector. However, I can only write if(collision.gameObject.CompareTag()), but that’s not working, because gameObject is this empty object that only carrying the script file. I can not write if(collision.mainPlayer.CompareTag()). How can I use those references in this method? Please help.

Referencing GameObjects, Scripts, variables, fields, methods (anything non-static) in other script instances or GameObjects:

It isn’t always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.

Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.

That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.

NOTE about your code in Start():

Keep in mind that using GetComponent() and its kin (in Children, in Parent, plural, etc) to try and tease out Components at runtime is definitely deep into super-duper-uber-crazy-Ninja advanced stuff.

Don’t use all those Get/Find things unless you absolutely MUST use them. And when you do decide you’re ready to do it, here’s the bare minimum of stuff you absolutely MUST keep track of if you insist on using these crazy Ninja methods:

  • what you’re looking for:
    → one particular thing?
    → many things?
  • where it might be located (what GameObject?)
  • where the Get/Find command will look:
    → on one GameObject? Which one? Do you have a reference to it?
    → on every GameObject?
    → on a subset of GameObjects?
  • what criteria must be met for something to be found (enabled, named, etc.)
  • if your code expects one instance and later you have many (intentional or accidental), does it handle it?

If you are missing knowledge about even ONE of the things above, your call is likely to FAIL.

If you have issues, start debugging. We know the above methods will ALWAYS work so find out what you’re doing wrong before bothering to make a forum post.

This sort of coding is to be avoided at all costs unless you know exactly what you are doing.

Botched attempts at using Get- and Find- are responsible for more crashes than useful code, IMNSHO.

If you run into an issue with any of these calls, start with the documentation to understand why.

There is a clear set of extremely-well-defined conditions required for each of these calls to work, as well as definitions of what will and will not be returned.

In the case of collections of Components, the order will NEVER be guaranteed, even if you happen to notice it is always in a particular order on your machine.

It is ALWAYS better to go The Unity Way™ and make dedicated public fields and drag in the references you want.

1 Like

OnCollisionEnter will only get called on the same game object where the collider is. You’ll have to make another script on that game object and then communicate with this main script through that.

2 Likes

This doesn’t make much sense. The only behaviour component that can actually detect collisions is the Rigidbody component. It’s responsible to generate and send those OnCollision messages. It only sends them to scripts on the same object as the rigidbody, and / or to the gameobject with the collider attached. An empty gameobject will never receive any OnCollision messages. That’s not how the Rigidbody component works. It does not send messages to some arbitrary unrelated object.

So it’s not quite clear what your goal is, but the setup that you describe just would not work.

2 Likes

I really appreciate your time and giving some great advices. In this way will, I will get references from another script file to the script I work on and I think I understand why it is called oop better now .