How to use the component class?

Hello.

I know the standard way to add a component to a gameobject with .addcomponent().

But is it also possible to instantiate a component with a class, monobehaviour script, and add it to a gameobject.
I tried some attempts but not sucesful.

eg:

Component myComp = new Component();

//reference my script
myComp = ?

//or this worked too, forgot it's inheritance could have been :component
Component myScript = new Script_myUI_Text();

//but then didn't work
GameObject g = new GameObject();
g.addcomponent<myScript>();

How do you use the Component class?

Since the component can only be on a game object, I’m not even sure if you can make one like that. That’s for the component part alone.
For the script (monobehaviour), those shouldn’t be created with the ‘new’ keyword.

And as you said, you know about addcomponent - which can do that, so it all works out in the end. :slight_smile: