Unity:NullReferenceException OnCollisionEnter2D

我是Unity的新手,我很难理解为什么我的OnCollisionEnter2D方法上出现nullreferenceException。现在,我的程序中有一个播放器对象,该对象刚从重力坠落并碰到了桨状对象并弹起。每当玩家物体碰到球拍时,得分应增加一。当我按下播放按钮时,播放器对象掉落,但没有分数更新,并且我得到了NullReferenceException。如果这看起来很基础,我深表歉意,但是我患有自闭症,只想学习编程游戏。任何帮助表示赞赏。

Protocol type 'nsorderedset.Element' (aka 'Any') cannot conform to 'Hashable' because only concrete types can conform to protocols
public class PlayerController : MonoBehaviour
{

    [SerializeField] private bool _jumpOn;
    public ScoreController scoreController;


    // Use this for initialization
    void Start ()
    {

    }

    // Update is called once per frame
    void FixedUpdate ()
    {
        if (!_jumpOn) return;

        Vector2 newPosition = transform.position;
        newPosition.y += 1f * Time.deltaTime;
        transform.position = newPosition;
    }

    private void OnCollisionEnter2D(Collision2D coll)
    { 
            Debug.Log("Hit Paddle!");
            _jumpOn = true;
           // Add one to score when you hit the paddle. 
           //Line below is where the error is being called
            scoreController.IncrementScore();        
    }
}

当我的播放器对象碰到桨时,它应该每次将分数更新1,但是所有发生的事情是,状态如下:

NullReferenceException:对象引用未设置为对象的实例 PlayerController.OnCollisionEnter2D(UnityEngine.Collision2D coll)(位于Assets / _Scripts / PlayerController.cs:38)

L_KJJ 回答:Unity:NullReferenceException OnCollisionEnter2D

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3140675.html

大家都在问