call a function from an object that has been collided with?

Hello.

I have two objects. Player, and statue. When the player collides with the statue, I want to be able to call a function that is on a script attached to the statue. There will be many statues in the game, so I want to only call the function on that one specific statue.

Currently I have the collision set up between the player and the statue, and it is all detected fine. I can get the statue gameobject from using collsion.gameObject. But I want to call the script that is on the statue, from another piece in the code (not the collision code). How would I call this script using the gameObject obtained from the collsiion.gameObject. Or is there another way to do this.? thanks

Edit : I may have found a way using

statueScript = collision.gameObject.GetComponent();

I am still testing this out, but think it is working. If anyone reading this knows if this should work could you please let me know.

Why not just have script on the statue detect when it collides with the player? Then call it’s own function.

Edit: Yeah, the thing you are trying should work too, but you might end-up with a lot of random object-handling routines on the player when maybe it would be easier if other objects just handle their own business

1 Like