Cast problem in C#

Hi all !

Here is my problem (very simplified). I don’t know if it’s possible or not.
I can drag and drop a C# Script in inspector thanks to this class.

[System.Serializable]
public class AreaInfo
{
     public UnityEngine.MonoScript _Instance;    
    // others members
}

In my hierarchy, i’ve attached the following script on a gameobject :

public class Game : MonoBehaviour
{
    public AreaInfo[] areaInfos;
     
    void Start() 
   {
       foreach(AreaInfo info in areaInfos)
       {
          
          // my dream would be : 
          NameClass toto = new ( info._Instance.GetClass() );  // the code is totally wrong, but i'm looking for if a solution  exists

      }
  }
}

I would like to instantiate an instance according to the type of Script (All my scripts inherits from MonoBehaviour) that i drag and drop in the inspector… Is it possible ?

thanks in advance !

MonoBehaviours are not “newed”, their construction inside of unity happens through GameObject.AddComponent<>() (or the non generic).

I guess you are getting a warning or error at the time due to the new there?

Yes, you’re right dreamora.

Thanks to the _Instance variable , i’ve got this function : GetClass(), which returns a System.Type …

foreach( ) 
     GameObject go = new GameObject(); 
     go.AddComponent< typeof( info._Instance.GetClass() > ( );  // something like that

I really do improve my c# skills ^^