call Script with the same name but other code

Hello! I want to call a function when 2 objects collide. Im doing this with “collider.getComponent().myClassName()” but now i want to have different code in myClassName. So if GameObject1 collides, it runs the code written in myClassName in GameObejct1 (for example "Debug.Log(“Hello”) and if GameObject2 collides, it runs the code written in myClassName in GameObject2, which is diffrent to the one in GameObject1 (for example Debug.Log(“Bye”). Is there a way to edit the script for every GameObject but with the same name? Or any other way to do this? Sorry for bad explanation.

You can do this trivially with interfaces.

In the example above, your myScriptName type would be the interface, and it would have its “DoYourThing()” method, which could be implemented in two different MonoBehaviour classes, one for each type of thing.

Using Interfaces in Unity3D:

Check Youtube for other tutorials about interfaces and working in Unity3D. It’s a pretty powerful combination.