System.Activator.CreateInstance返回nullActivator、System、null、CreateInstance

2023-09-04 03:07:30 作者:危险大叔

我的问题是,的CreateInstance 返回null。

The problem I have is that CreateInstance returns null.

下面是code:

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

//combo.SetSpecialAttack(spattack);
}

攻击 SpecialAttack 是存储的基本信息两类,并从 UnityEngine继承.Object

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

Attmono spattmono 都是 MonoScript 阵列, attmono 能容纳16个和 spattmono 能容纳4。

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);
}

您能想到 MonoScript 的一样东西,包含什么类类型的对象。 我已经检查每一项与 Debug.Print 语句,无一不是不是空被分配的时候。

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.

下面是 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.

推荐答案

假设Activator.CreateInstance没有实际工作正常,那么就必须有sometihng不妥提供给方法调用的参数。 (较早因此这个问题)。

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).

由于缺乏传递到的CreateInstance我猜,但我怀疑的值传递为空或为一个类型,要么是内部的,但居住在不同的装配或类似的访问问题的实际价值的知识。

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.