Best method of calling a function in an unknown script.

First, sorry for the ambiguous question. I’m having a hard time even figuring out how to ask it.

So to start I have a bunch of objects, and I want to animate the object via a script attached to the object when OnTriggerEnter() is called in another part of the game. The problem is that each object and script are different from each other. I don’t have 50 of the same thing.

Of all of these objects, only one will be animated at a time. But since they can be chosen by a player I don’t know which object I need to animate ahead of time. The only thing I can think of is using a bunch of if/else statements.

if(object 1)
{
	object1Script.runAnimation();
}
else if(object 2)
{
	object2Script.runAnimation();
}
else if(object 3)
{
	object3Script.runAnimation();
}

This just seems like its going to get real messy when I have 50 or 60 if else statements. I guess I’m wondering if there is a way to access a random script and call a function in it? This way I could have the same function copy and pasted at the start of each script and just call it from OnTriggerEnter(). This seems like it would require a variable of type “Script”, which even with my lack of experience, doesn’t seem like the right way to do things either.

if(randomobject)
{
	randomObjectScript.runAnimation();
}

The simplest way is to just use SendMessage.

I guess I’m not the first person to ask this. Seems like it should work.

Create a variable of type C# Script