Can I attach a public Monoscript variable as a script component to a gameobject instantiation?

7621591--947827--upload_2021-11-1_21-56-39.png
using this line:

gameObject.AddComponent<attachScript>();

Unity lets me set this all up but then I can not get the assignment code line correct.

Yes, you can’t use a variable in place of a Generic Type parameter.

Look at the overloads for AddComponent: Unity - Scripting API: GameObject.AddComponent

One overload allows a Type variable instead. You should be able to grab the type through some means and use that in the overload. I can’t remember the proper syntax off the top of my head; nor am I at home to test for it admittedly.

I found references on Reddit and came up with this very simple guarantee:

public MonoScript attachScript;

System.Type m_ScriptClass = attachScript.GetClass();

gameobject.AddComponent(m_ScriptClass);

After this code then methods access can be gained.