Search Unity

Activator.CreateInstance error when exporting

Discussion in 'Web' started by boolean01, Apr 16, 2018.

  1. boolean01

    boolean01

    Joined:
    Nov 16, 2013
    Posts:
    92
    I'm trying to use reflection to instantiate some classes. I have some class names that I store in a database, and then at runtime take the class name and use:

    Code (CSharp):
    1. var thingScript = (BaseClassOfThingToReflect)Activator.CreateInstance(null, "ThingToReflect").Unwrap();
    It works fine in the editor, but when I export to WebGL I get ''A type load exception has occurred' error.

    According to the docs, the "System.Reflection.Emit' namespace isn't supported, however I'm pretty sure the method I'm using is from the root 'System' namespace. If I use the following syntax it works fine (I assume because I'm already referencing the type as the T parameter):

    Code (CSharp):
    1. var thingScript = Activator.CreateInstance<ThingToReflect>();
    Is what I'm trying to do supported? If not, are there any alternatives to this? The way I setup my game is pretty dependent on saving script names to the database and initializing them later.

    (using Unity 2017.3.1f1).
     
  2. wumeng2019

    wumeng2019

    Joined:
    Jul 11, 2019
    Posts:
    3
    I have the same problem in WebGL.