Hi OnixFang,
Since your solution is a bit different than Gary’s, it does not matter how Gary intended it to be. If you are interested in his solution, use his. If you are interested in making your own solution work, don’t worry about Gary’s.
Your GetInstance method is perfectly fine. However, it does not solve the problem with the NullReferenceException error. instance could still be null. Your code never checks its value, and, from what I see, it never assigns any value to instance, hence instance remains null and will always throw a NullReferenceException if you try to access the non-existent object.
Assign a reference, and your problem will very likely be solved.
Take Unity’s execution order into consideration when assigning the reference. Ideally, the reference should get assigned before other code parts try to access it, for example, in Start(). The Awake() method might be a good place to look for an ScoreKeeper object.
Did this help?
See also:
- Forum User Guides : How to mark a topic as solved