当我调用一个非静态方法时,我得到了这个错误:
/Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222) NullReferenceException RealmRecruitment.ResolveRealmFestival () (在资产/脚本/卡片操作/领域卡片/行为/RealmRecruitment.cs:14) System.Reflection.MonoMethod.Invoke (System.Object obj、BindingFlags invokeAttr、System.Reflection.Binder绑定程序、System.Object[]参数、System.Globalization.CultureInfo区域性)(在System.Reflection.MonoMethod.Invoke中,调用的目标抛出了TargetInvocationException: Exception )。/Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232) System.Reflection.MonoMethod.Invoke (System.Object obj,BindingFlags invokeAttr,System.Reflection.Binder binder,System.Object[]参数,System.Globalization.CultureInfo培养) (at System.Object System.Reflection.MethodBase.Invoke (System.Object obj,(在/Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115) RealmCardManager.OnMouseDown () (在资产/脚本/卡片操作/领域卡/RealmCardManager.cs:48)) System.Object[]参数
下面是调用单个方法的代码,如上面的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更改为静态,那么代码运行良好(当然,我会对RealmFestival中的调用进行其他更改)。但是,我不能将该方法作为静态的。
我试着把政治问题放到一个GameObject上,让它独立存在,但它仍然没有改变错误信息。
提前谢谢你
应BatteryBackupUnit的请求(并在其协助下),我将其作为InnerException:
System.Reflection.TargetInvocationException:调用的目标引发了异常。-> System.Exception: at (包装器托管到本机) at RealmFete.ResolveRealmFete () 0x0001b (F:\System.Object\Online\Assets\Script\Card Online\ Cards\Behaviors\RealmFete.cs:14 at (包装托管到本地) System.Reflection.MonoMethod:InternalInvoke (object,object[],System.Exception&) at System.Reflection.MonoMethod.Invoke (System.Object obj,BindingFlags invokeAttr,System.Reflection.Binder binder,System.Object[]参数),( /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:222中的System.Globalization.CultureInfo区域性) 0x000d0 --内部异常堆栈跟踪的结束--在System.Reflection.MonoMethod.Invoke (System.Object obj、BindingFlags invokeAttr、System.Reflection.Binder绑定、System.Object[]参数,/Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MonoMethod.cs:232 at System.Reflection.MethodBase.Invoke (System.Object obj,System.Object obj)( /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115中的System.Object[]参数) 0x00000在F中的RealmCardManager.OnMouseDown () 0x0003e中:\System.Object[]\Online代码\Assets\Script\卡片操作\领域Cards\RealmCardManager.cs:49
发布于 2015-09-22 13:18:30
根据统一文献
请注意,有经验的程序员:您可能会惊讶于对象的初始化没有使用构造函数完成。这是因为对象的构造是由编辑器处理的,而不是像您预期的那样在游戏开始时进行。如果您试图为脚本组件定义一个构造函数,它将干扰团结的正常操作,并可能导致项目出现重大问题。
在您的示例中,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;https://stackoverflow.com/questions/32709927
复制相似问题