Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.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# 当玩家与和对象碰撞时出错。统一_C#_Unity3d - Fatal编程技术网

C# 当玩家与和对象碰撞时出错。统一

C# 当玩家与和对象碰撞时出错。统一,c#,unity3d,C#,Unity3d,我正在为我正在制作的游戏寻找问题解决方案 有一个玩家需要躲避障碍物,我想让我的分数在玩家与障碍物碰撞时停止,而且我还想做一个高分计数 但现在,当玩家与障碍物相撞时,我得到了一个错误 播放器的代码是 using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public class P

我正在为我正在制作的游戏寻找问题解决方案

有一个玩家需要躲避障碍物,我想让我的分数在玩家与障碍物碰撞时停止,而且我还想做一个高分计数

但现在,当玩家与障碍物相撞时,我得到了一个错误

播放器的代码是

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class Player : MonoBehaviour

{
    public GameObject deathcanvas;
    public float moveSpeed = 600f;
    public Text scoreText;
    float movement = 0f;
    private bool isDead = false;
    // Update is called once per frame
    void Update()
    {           
        movement = Input.GetAxisRaw("Horizontal");
    }

    private void FixedUpdate()
    {
        if (isDead)
            return;
        transform.RotateAround(Vector3.zero, Vector3.forward, movement * Time.fixedDeltaTime * -moveSpeed);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        Death();
    }
        private void Death()
    {
        isDead = true;
        GetComponent<Score> ().OnDeath(); 
        deathcanvas.SetActive(true);
    }
}
还有一个障碍产卵器的代码,但我不认为它是必要的。 当玩家与障碍物碰撞时的错误是

NullReferenceException: Object reference not set to an instance of an object
Player.Death () (at Assets/scripts/Player.cs:36)
Player.OnTriggerEnter2D (UnityEngine.Collider2D collision) (at Assets/scripts/Player.cs:31)

我该怎么办?

这里有些奇怪的事情,为什么你有两个死亡的功能和两本死亡之书?它很有用,因为分数和其他脚本一样在播放器上,所以您可以将所有内容放在播放器脚本中,并访问画布和分数文本,而无需使用公共函数和大量变量。如果它仍然是一个错误,那么我们可以看到另一个

听起来像
GetComponent()
没有返回任何内容,因为该组件与
Player
脚本不在同一个对象上,或者
deathcanvas
没有被引用。。。
NullReferenceException: Object reference not set to an instance of an object
Player.Death () (at Assets/scripts/Player.cs:36)
Player.OnTriggerEnter2D (UnityEngine.Collider2D collision) (at Assets/scripts/Player.cs:31)