Hi! I’m trying to figure out how to properly get a class type and make a new one of it… I know sort of what im trying to do but can’t come up with the right words to find a solution on coding forums. Maybe it’s not possible.
I have a ScriptableObject that holds a slot for a base behavior class.
“public EnemyFunction functionScript” <-slot
When i make an SO for a new enemy, ill drag a different class inheriting the base class in for that enemy.
“public class EFImpA : EnemyFunction” <-specific inherited class
When the generic enemy takes in the SO data, id like to create a new class of this type, but im not sure how to ‘get’ this type to create from this slot with C#.
Something like…
public EnemySO enemydata;
EnemyFunction myfunctionscript;
void Setup()
{
myfunctionscript = new 'enemydata.functionScript.GetType'(); //sort of paraphrasing what im trying to do
}
Down the line id like to create a JSON reader or parser for this sort of thing instead, but for now Id just like to get a specific class for the enemy to run inside its generic controller.
If I’m reading you right, you might want to look at the System.Activator.CreateInstance method, which you can use to make an instance of a class by means of a Type value. Note this won’t work on anything inheriting from UnityEngine.Object.
You aren’t trying to get Unity to expose polymorphic data are you? That won’t work by default without plugins or extensive editor work.
When using Instantiate you would create a clone of the instance, so you would copy all the data into the new instance as well. This is not really possible for monobehaviours. Well you could use Instantiate on a MonoBehaviour as well, but it would clone the whole gameobject (including all other components and sub-gameobjects).