Rethrow as TargetInvocationException错误

时间:2015-09-22 06:33:23

标签: c# unity3d

当我调用非静态方法时,我收到此错误:

  

NullReferenceException RealmRecruitment.ResolveRealmFestival()(在Assets / Scripts / Card Manipulation / Realm)   卡/行为/ RealmRecruitment.cs:14)   System.Reflection.MonoMethod.Invoke(System.Object obj,BindingFlags   invokeAttr,System.Reflection.Binder binder,System.Object []   参数,System.Globalization.CultureInfo文化)(at   /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222)Rethrow as TargetInvocationException:Exception已被抛出   调用的目标。 System.Reflection.MonoMethod.Invoke   (System.Object obj,BindingFlags invokeAttr,System.Reflection.Binder   binder,System.Object []参数,System.Globalization.CultureInfo   文化)(at   /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232)System.Reflection.MethodBase.Invoke(System.Object obj,   System.Object []参数)(at   /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115)ErealmCardManager.OnMouseDown()( at Assets / Scripts / Card   Manipulation / Realm Cards / RealmCardManager.cs:48 )   UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32)已

以下是调用各个方法的代码,如上面的RealmCardManager.OnMouseDown()消息所示,该消息是斜体。 (注意:我删除了不相关的代码):

public class RealmCardManager : MonoBehaviour {
    void OnMouseDown(){     
        Type classType = Type.GetType(CardBehavior);
        ConstructorInfo rCon = classType.GetConstructor(Type.EmptyTypes);
        object cardResult = rCon.Invoke(new object[]{});
        MethodInfo theMethod = classType.GetMethod("Resolve" + CardBehavior, BindingFlags.Instance | BindingFlags.Public);  
        theMethod.Invoke(cardResult, null);
    }
}

上面的代码基于字符串CardBehavior调用单个方法,在这种情况下调用的方法如下

public class RealmFestival : MonoBehaviour {
    public PoliticalProblems pP;
    public void ResolveRealmFestival(){
        pP = gameObject.AddComponent<PoliticalProblems>();
        pP.AdjustHeresy(false);
    }
}

最后,这会调用以下代码:

public class PoliticalProblems: MonoBehaviour {
    public void AdjustHeresy(bool increase){
        if(increase){
            GameData.RealmLevels[2] ++;
        } else {
            GameData.RealmLevels[2] --;
        }
    }
}

如果我将AdjustHeresy更改为Static,则代码可以正常工作(当然我会对RealmFestival中的调用进行其他更改)。但是,我不能将该方法作为静态。

我已经尝试将政治问题放在GameObject上并让它独立并且它仍然不会更改错误消息

提前谢谢

在请求(并借助于)BatteryBackupUnit的情况下,我将此作为InnerException:

  

System.Reflection.TargetInvocationException:调用目标抛出了异常。 ---&GT; System.Exception的:     at at(wrapper managed-to-native)UnityEngine.Component:get_gameObject()     在F:\ Unity Projects \ Online Mulitplayer Code \ Assets \ Scripts \ Card Manipulation \ Realm Cards \ Behaviors \ RealmFete.cs中的RealmFete.ResolveRealmFete()[0x0001b]:14     at at(wrapper managed-to-native)System.Reflection.MonoMethod:InternalInvoke(object,object [],System.Exception&amp;)     at System.Reflection.MonoMethod.Invoke(System.Object obj,BindingFlags invokeAttr,System.Reflection.Binder binder,System.Object [] parameters,System.Globalization.CultureInfo culture)[0x000d0] in / Users / builduser / buildslave / mono -runtime和 - classlibs /建造/ MCS /类/ corlib /的System.Reflection / MonoMethod.cs:222     ---内部异常堆栈跟踪结束---     at System.Reflection.MonoMethod.Invoke(System.Object obj,BindingFlags invokeAttr,System.Reflection.Binder binder,System.Object [] parameters,System.Globalization.CultureInfo culture)[0x000eb] in / Users / builduser / buildslave / mono -runtime和 - classlibs /建造/ MCS /类/ corlib /的System.Reflection / MonoMethod.cs:232     at System.Reflection.MethodBase.Invoke(System.Object obj,System.Object [] parameters)[0x00000]在/ Users / builduser / buildslave / mono-runtime-and-classlibs / build / mcs / class / corlib / System中。反射/ MethodBase.cs:115     在F:\ Unity Projects \ Online Mulitplayer Code \ Assets \ Scripts \ Card Manipulation \ Realm Cards \ RealmCardManager.cs中的RealmCardManager.OnMouseDown()[0x0003e]:49

1 个答案:

答案 0 :(得分:1)

根据Unity documentation

  

有经验的程序员注意:你可能会感到惊讶   使用构造函数不能完成对象的初始化。   这是因为对象的构造由编辑器处理   并且不会像您期望的那样在游戏开始时发生。   如果您尝试为脚本组件定义构造函数,它将会   干扰Unity的正常运行并可能导致重大问题   该项目存在问题。

在您的情况下,RealmFestival无论如何都不需要MonoBehaviour:它只是将PoliticalProblems组件附加到游戏对象。事实上,它也不需要成为一个类 - 在这种情况下静态函数就足够了(除非你需要跟踪状态 - 但看起来这可以由PoliticalProblems组件完成):

public class CardEffects
{
    public static void ResolveRealmFestival(GameObject gameObject)
    {
        var politicalProblems = gameObject.AddComponent<PoliticalProblems>();
        politicalProblems.AdjustHeresy(false);
    }
}

从您的代码中不清楚字符串CardBehavior来自哪里(我假设它是卡片对象的属性),但如果您使用Action<GameObject>代替一个字符串,你可以存储对函数的引用,而不必使用反射查找它:

public class Card
{
    //public string CardBehaviour;
    public Action<GameObject> CardBehaviour;

    public void ApplyCardEffect(GameObject target)
    {
        // Instead of reflection, we can simply invoke the Action directly - assuming it's not null:
        CardBehaviour(target);
    }
}

卡初始化代码会相应更改:

//card.CardBehaviour = "RealmFestival";
card.CardBehaviour = CardEffects.ResolveRealmFestival;