C# Script as a Component Variable

Hello Guys,

I got a little Problem: I would like to safe a script (that is a component of a gameobject) into a variable in an other script.

For example:

GameObject XYZ got 2 scripts like


public Component MyScriptXZY;

public void MyFunction{



public void StuffShouldHappen{
 //le totally awesome stuff


I can simply drag my ScriptB of the Gameobject into the Componentvariable of ScriptA, but I can’t do anything with the variable. I got no access to he mehtods of ScriptB.
How can I adress the methods of Scrip?

My mainproblem is, thats why I want to do it like that, is that the script within the Component-Variable might be different, depending on the gameobject.
But since I want to adress the method with on line like Component.MyMethod() instead of an anoying


Is there a way to do it the way I need it?
Thanks for any help!

You need to learn about polymorphism. There is no built-in class that you can derive from, which can do the specific things you need. So, make your own class.

Component was a poor choice; you knew that your object was going to be a MonoBehaviour. The docs tell you that MonoBehaviour derives from Behaviour, which derives from Component. There was no point to travel so far up the hierarchy. Be specific. Create your own class that derives from MonoBehaviour, and derive from that as necessary.

You can also just do:

MonoBehavior theScript;