简体   繁体   English

文本类型的 object 已被破坏,但您仍在尝试访问它 - Unity

[英]The object of type Text has been destroyed but you are still trying to access it - Unity

I have a game that has a main play scene and a game over scene.我有一个游戏,它有一个主要的游戏场景和一个游戏结束场景。 The Game over scene has a button that simply starts the main scene again.游戏结束场景有一个按钮,可以简单地再次启动主场景。

Everything works fine for the first play, but when restarting through the game over screen and trying to add to my score I get the following error:第一次玩时一切正常,但是当通过屏幕重新开始游戏并尝试添加到我的分数时,我收到以下错误:

The object of type Text has been destroyed but you are still trying to access it.

I'm not sure why this is, I've tried logging out the referenced text object in the update and it looks fine.我不知道为什么会这样,我已经尝试在更新中注销引用的文本 object,它看起来很好。

I'm setting the highScore by dragging the text UI component to the script in the editor.我通过将文本 UI 组件拖到编辑器中的脚本来设置highScore I've also tried using GetComponent but run into the same issue.我也尝试过使用GetComponent但遇到了同样的问题。

Heres the relevant script:继承人的相关脚本:

public class ScoreHandler : MonoBehaviour
{
  public Text highScore;
  private int score = 0;

  private int basicEnemyValue = 10;
  private int speedyEnemnyValue = 15;
  private int bossEnemyValue = 50;
  private int startingHighScore;

  public static event Action<int> OnScoreChange;

  void OnEnable() {
    BasicEnemy.OnBasicEnemyDestroyed += BasicEnemyDestroyed;
    TankEnemy.OnTankEnemyDestroyed += TankEnemyDestroyed;
    SpeedyEnemy.OnSpeedyEnemyDestroyed += SpeedyEnemyDestroyed;
  }


  void onDisable() {
    BasicEnemy.OnBasicEnemyDestroyed -= BasicEnemyDestroyed;
    TankEnemy.OnTankEnemyDestroyed -= TankEnemyDestroyed;
    SpeedyEnemy.OnSpeedyEnemyDestroyed -= SpeedyEnemyDestroyed;
  }

  void Start () {
    startingHighScore = PlayerPrefs.GetInt ("highscore", 0);
    highScore.text = "Score: " + score;
  }


  void Update () {
    Debug.Log(highScore); // Looks good here, has a ref to the correct component UnityEngine.Ui.Text
    Debug.Log(highScore.text); // Same as above - has what's set in start
  }

  private void checkHighScore(int score) {
    if (score > startingHighScore) {
      PlayerPrefs.SetInt ("highscore", score);
    }
  }

 // ***HERE IS THE PROBLEM FUNCTION***
  private void updateScoreText() {
    Debug.Log(highScore); // null


    highScore.text = "Score: " + score;
    checkHighScore(score);
    OnScoreChange?.Invoke(score);
  }

  private void BasicEnemyDestroyed() {
    score += basicEnemyValue;
    updateScoreText();
  }

  private void TankEnemyDestroyed() {
    score += bossEnemyValue;
    updateScoreText();
  }

  private void SpeedyEnemyDestroyed() {
    score += speedyEnemnyValue;
    updateScoreText();
  }

}

I'm a bit confused as everything looks fine in the update, but seems to loose the component when I call the function on it.我有点困惑,因为更新中的一切看起来都很好,但是当我在其上调用 function 时似乎松动了组件。 I've tried to add dontDestoryOnLoad() to the UI, but that adds a number of other issues with other UI components not being defined.我尝试将 dontDestoryOnLoad() 添加到 UI 中,但这会增加许多其他未定义的 UI 组件的其他问题。

onDisable -> OnDisable onDisable -> OnDisable

It's case-sensitve, so your event handlers are never removed.它区分大小写,因此您的事件处理程序永远不会被删除。


FYI there is a shortcut to insert these messages in Visual Studio: Ctrl Shift M仅供参考,在 Visual Studio 中插入这些消息有一个快捷方式: Ctrl Shift M

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 unity 'GameObject' 类型的 object 已被破坏,但您仍在尝试访问它 - unity The object of type 'GameObject' has been destroyed but you are still trying to access it 如何修复 Unity 中的“&#39;GameObject&#39; 类型的对象已被销毁,但您仍在尝试访问它”错误? - How to fix "the object of type 'GameObject' has been destroyed, but you are still trying to access it" error in Unity? 如何修复“'GameObject' 类型的 object 已被破坏,但您仍在尝试访问它。” 统一 - How I can fix the “The object of type 'GameObject' has been destroyed but you are still trying to access it.” Unity “ GameObject”类型的对象已被破坏,但您仍在尝试访问它 - The object of type 'GameObject' has been destroyed but you are still trying to access it “CanvasGroup”类型的对象已被销毁,但您仍在尝试访问它 - The object of type "CanvasGroup" has been destroyed but you are still trying to access it “BallMovement”类型的 object 已被销毁,但您仍在尝试访问它 - The object of type 'BallMovement' has been destroyed but you are still trying to access it 错误 MissingReferenceException:“GameObject”类型的 object 已被破坏,但您仍在尝试访问它 - Error MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it MissingReferenceException:类型为“攻击者”的对象已被破坏,但您仍在尝试访问它 - MissingReferenceException: The object of type 'Attacker' has been destroyed but you are still trying to access it “healthscript”类型的游戏 object 已被销毁,但您正在尝试访问它 - The game object of type “healthscript” has already been destroyed but you are trying to access it Unity “GameObject”类型的 object 已被销毁 - Unity The object of type “GameObject” has been destroyed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM