#Unity and C#, getting type of child SO in a way that CreateInstance<Type>() can accept

3 messages · Page 1 of 1 (latest)

sly marsh
#

I'm just learning C# and Unity and I'm having a hell of a time trying to figure out the proper syntax here:

var assets = AssetDatabase.LoadAllAssetsAtPath(AssetDatabase.GetAssetPath(this));
foreach (var a in assets)
{
    if (a == null)
    {
        var asset = CreateInstance<a.GetType()>();
        asset.name = "Stats";
        asset.Instantiate(this);

        AssetDatabase.AddObjectToAsset(asset, this);
    }
}

My issue is the var asset = CreateInstance<a.GetType()>();
I'm trying create instances of multiple scriptable objects and assign them to a parent scriptable object, but I can't seem to get the target object type in a way that CreateInstance<Type>() will accept. Can anyone help me out here?

tawdry goblet
#

Just a guess since I never used your way to Instantiate objects but does adding new keyword help?
It would be better to state your problem more clearly like is it an error (Better if you can provide what error it exactly is) or it simply doesn't work?

wintry crag
#

this won't work
the only work around for this is using reflections