提问者:小点点

我试图更新一个score变量,但得到了一个NullReferenceException(不是重复的)[DUPLICATE]


(是的,我知道上面说这是复制品。我也知道这个错误意味着什么。我试图弄清楚为什么现在的变量是null)

未将对象引用设置为对象的实例。得分。更新()

获取此脚本的错误。显然现在的变量是null,我试图找出原因。

这是我的记分脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;

public class Score : MonoBehaviour
{
    public TextMeshPro scoreText;
    public Present present;

    // Start is called before the first frame update
    void Start()
    {
        present = FindObjectOfType<Present>();
    }

    // Update is called once per frame
    void Update()
    {
        scoreText.text = present.score.ToString();
    }
}

这是我的Present脚本(带有分数变量的脚本):

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

public class Present : MonoBehaviour
{
    public Vector2 velocity;
    private double deletionZone = 15;
    public int score = 0;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        // move to the left
        transform.Translate(velocity * Time.fixedDeltaTime);
        if (transform.position.x <= -deletionZone)
        {
            Destroy(gameObject);
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Player")
        {
            Destroy(gameObject);
            score += 10;
        }
    }
}

共1个答案

匿名用户

我认为这是因为你用大写字母P命名当前字段,它应该是lowee case-Present。所以公众出席;

相关问题