Advertisement
Guest User

Untitled

a guest
Aug 27th, 2024
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. You are missusing the singleton pattern.
  2.  
  3.  
  4. The correct use would be this:
  5.  
  6. In `ScoreKeeper`
  7.  
  8. ```lang-cs
  9. public class ScoreKeeper : MonoBehaviour
  10. {
  11. public static ScoreKeeper Instance { get; private set; }
  12.  
  13. private void Awake()
  14. {
  15. if (Instance != null && Instance != this) // if we are the instance this is fine
  16. {
  17. Destroy(this);
  18. return;
  19. }
  20.  
  21. Instance = this;
  22. DontDestroyOnLoad(this);
  23. }
  24. }
  25.  
  26. ```
  27.  
  28. Then when accessing it, don't hold on to a reference, just do this each time.
  29.  
  30. ```lang-cs
  31. ScoreKeeper.Instance.ResetScore();
  32. ```
  33.  
  34. Your error was occuring becuase `LevelManager`'s reference was somehow becoming null, likley because loading into the game made the original `ScoreKeeper` delete itself due to the error in it's awake logic
  35.  
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement