System.Activator.CreateInstance returning null

孤街醉人 提交于 2019-12-12 09:48:44

问题


The problem I have is that CreateInstance returns null.

Here is the code:

if(spattmono[0] != null)
{
    if((SpecialAttack) System.Activator.CreateInstance(
        spattmono[0].GetClass()) == null)
    {
    Debug.Log("DUMB ACTIVATOR!!!");
    }

//combo.SetSpecialAttack(spattack);
}

Attack and SpecialAttack are both classes that store basic information, and inherit from UnityEngine.Object.

Attmono and spattmono are both MonoScript arrays, attmono being able to hold 16 and spattmono being able to hold 4.

They get there information from these.

for(int at = 0; at < numberOfAttacks; ++at )
{
    attmono[at] = (MonoScript) EditorGUILayout.ObjectField(attmono[at], 
        typeof(MonoScript), false);
}

for(int spat = 0; spat < 4; ++spat )
{
    spattmono[spat] = (MonoScript) EditorGUILayout.ObjectField(
        spattmono[spat], typeof(MonoScript), false);
}

You could think of MonoScript just as something that holds what class type the object is. I have checked each of these with Debug.Print statements and both are not null when being assigned.

Here is the SpecialAttack code.

public class SpecialAttack : UnityEngine.Object 
{
    public string Name;

public int Damage;
public int Force;
public float Cooldown;

public SpecialAttack() 
    { }

public virtual bool Run() 
    {
    return false;
}
}

Ive recently tested this

if((SpecialAttack)System.Activator.CreateInstance(spattack.GetType()) == null) 
{
    Debug.Log("DUMB ACTIVATOR!!!");
}

And it was indeed null, so that makes me believe that the Activator cant find the type, so im not to sure what to do from here.


回答1:


Let's assume that Activator.CreateInstance does actually work correctly, then there must be sometihng wrong with the arguments supplied to the method call. (Hence the question earlier).

Due to lack of knowledge of the actual value passed to CreateInstance I'm guessing but I would suspect the value passed being null or being a type that is either internal but resides in a different assembly or similar access problems.



来源:https://stackoverflow.com/questions/7137884/system-activator-createinstance-returning-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!