【发布时间】:2017-03-01 08:39:14
【问题描述】:
我有工作项目,但一切都超出了标准资产。所以我需要将所有文件夹和项目移动到标准资产文件夹。当我将所有文件和文件夹移动到标准资产时,它给了我空引用错误。
我的项目中有一个命名空间,当我尝试访问这个命名空间时,它给了我 null 错误。
这是我的命名空间代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Assets.Scripts
{
public interface IInputDetector
{
InputDirection? DetectInputDirection();
}
public enum InputDirection
{
Left, Right, Top, Bottom
}
}
这里是访问它的代码
IInputDetector inputDetector = null;
在启动方法中
void Start(){
inputDetector = GetComponent<IInputDetector>();
}
在更新方法中它给了我错误
var inputDirection = inputDetector.DetectInputDirection(); // Line 222
错误是这样的..
NullReferenceException:对象引用未设置为对象的实例 ControllerMovement.DetectJumpOrSwipeLeftRight () (在 Assets/Scripts/ControllerMovement.cs:222) ControllerMovement.Update () (在 Assets/Scripts/ControllerMovement.cs:106)
第 22 行在 var 行之上..
第 106 行来自我在 Update() 方法中调用的方法。
喜欢
void Update(){
DetectJumpOrSwipeLeftRight(); // line 106
}
【问题讨论】:
-
看起来你有一个 ControllerMovement 类型的对象,当调用 DetectJumpOrSwipeLeftRight 时它为空。然而,代码 sn-ps 不足以检查为什么会出现这种情况。您至少应该显示 detectInputs() 正在做什么,因为错误正在那里发生。
-
哦,对不起,我打电话给 DetectJumpOrSwipeLeftRight();在更新()中。
-
ContollerMovement 是我的类名,它指向我这一行 "var inputDirection = inputDetector.DetectInputDirection(); // 第 222 行" @Nebr
-
应该在 IInputDetector 中的 DetectInputDirection() 函数在哪里?
-
是的,DetectInputDirection() 从命名空间返回像 "LeftSwipe", "RightSwipe" , "Top and Bottom" 这样的值,所以当用户向任何方向滑动时,我可以检查它(左 == 左)或不。 @Alox
标签: c# unity3d namespaces