Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 NullReferenceException火球_C# 4.0_Unity3d - Fatal编程技术网

C# 4.0 NullReferenceException火球

C# 4.0 NullReferenceException火球,c#-4.0,unity3d,C# 4.0,Unity3d,我正在做这个游戏 我试着让球着火,撞到墙上弹跳,为了做到这一点,我按照他说的做了,我添加了一个脚本,并输入以下代码: public class BallMove : MonoBehaviour { private Rigidbody rb; public float ballVelocity = 800f; private bool isMove; // Use this for initialization void awake() { rb = GetComponent<

我正在做这个游戏

我试着让球着火,撞到墙上弹跳,为了做到这一点,我按照他说的做了,我添加了一个脚本,并输入以下代码:

public class BallMove : MonoBehaviour {


private Rigidbody rb;
public float ballVelocity = 800f;

private bool isMove; 
// Use this for initialization
void awake() {
    rb = GetComponent<Rigidbody> ();
}

// Update is called once per frame
void Update () {

    if (Input.GetButtonDown ("Fire1") && isMove == false) {
        transform.parent = null;
        isMove = true;
        rb.isKinematic = false;
        rb.AddForce (new Vector3(ballVelocity,ballVelocity,0));
    }
}
public class BallMove:monobhavior{
私人刚体;
公共浮球速度=800f;
私人布尔伊斯莫夫;
//用于初始化
无效唤醒(){
rb=GetComponent();
}
//每帧调用一次更新
无效更新(){
if(Input.GetButtonDown(“Fire1”)和&isMove==false){
transform.parent=null;
isMove=真;
rb.iskinetic=false;
rb.AddForce(新矢量3(球速,球速,0));
}
}
}

我理解这段代码的每一行,但是当我尝试玩这段代码时,我得到了一个nullReferenceException,我可以运行这个游戏,但是当我按下这个键时,我得到了一个错误,有人知道为什么吗?发生了什么?

您应该使用
Awake()
,而不是
Awake()
。在您的情况下,您使用的是“定制”功能,而不是Unity引擎使用的“官方”功能

因此,引擎无法自行启动该功能,在
Update()
中使用时,
rb
仍然为空

例如:

void Awake() {
    rb = GetComponent<Rigidbody> ();
}
void Awake(){
rb=GetComponent();
}
您应该使用
Awake()
,而不是
Awake()
。在您的情况下,您使用的是“定制”功能,而不是Unity引擎使用的“官方”功能

因此,引擎无法自行启动该功能,在
Update()
中使用时,
rb
仍然为空

例如:

void Awake() {
    rb = GetComponent<Rigidbody> ();
}
void Awake(){
rb=GetComponent();
}

比我快2秒,这就是答案,因为游戏对象也有一个刚体attached@ParadoxForge:接得好!是的,这是试图学习统一的人的典型错误。区分大小写通常被忽略:-)比我快2秒,这就是答案,因为游戏对象也有一个刚体attached@ParadoxForge:接得好!是的,这是试图学习统一的人的典型错误。区分大小写通常被忽略:-)