C
C#2y ago
rotkip

❔ MissingReferenceException: ... in Unity

This is the exception that occured: NullReferenceException: Object reference not set to an instance of an object Highscores.AddNewHighscore (System.String username, System.Int32 score) (at Assets/Scripts/Highscores.cs:22) I followed some tutorials for creating a game with a menu, online stored leaderboard and a game over UI. Here are the tutorials (they are a bit outdated): game ep 1) https://youtu.be/OspvMh3UWPU, game ep 2) https://youtu.be/5vov9H7RuF8, game ep 3) https://youtu.be/beWwwxn7dLQ, game ep 4) https://youtu.be/6nfaPDPA-Y8, leaderboard ep 1) https://youtu.be/KZuqEyxYZCc, leaderboard ep 2) https://youtu.be/9jejKPPKomg, menu) https://youtu.be/-GWjA6dixV4 Here is my code, I don't think that I follow any of the code conventions. https://paste.pythondiscord.com/zetovelida The game is about dodging falling blocks as long as you can, I added a highscore system and a menu.
Sebastian Lague
YouTube
Introduction to Game Development (E13: falling blocks game 1/4)
Welcome to episode thirteen of this introduction to game development in Unity with C#. In this episode we look at player movement for a simple 'avoid the falling blocks' game. Project files: https://github.com/SebLague/Intro-to-Gamedev If you'd like to support these videos, you can make a recurring monthly donation (cancellable at any time) t...
Sebastian Lague
YouTube
Introduction to Game Development (E14: falling blocks game 2/4)
Welcome to episode fourteen of this introduction to game development in Unity with C#. In this episode we instantiate the falling blocks and randomize their rotation and scale. Project files: https://github.com/SebLague/Intro-to-Gamedev If you'd like to support these videos, you can make a recurring monthly donation (cancellable at any time) ...
Sebastian Lague
YouTube
Introduction to Game Development (E15: falling blocks game 3/4)
Welcome to episode fifteen of this introduction to game development in Unity with C#. In this episode we create a player death condition, and increase the difficulty of the game over time. Project files: https://github.com/SebLague/Intro-to-Gamedev If you'd like to support these videos, you can make a recurring monthly donation (cancellable a...
Sebastian Lague
YouTube
Introduction to Game Development (E16: falling blocks game 4/4)
Welcome to episode sixteen of this introduction to game development in Unity with C#. In this episode we create a game over screen and export the game as a standalone build. Project files: https://github.com/SebLague/Intro-to-Gamedev If you'd like to support these videos, you can make a recurring monthly donation (cancellable at any time) thr...
14 Replies
friedice
friedice2y ago
probably some kind of unity specific issue, but code wise, this line here Highscores.AddNewHighscore(username, score); is causing the error, and HighScores is null check if Awake() is ever being called is my best guess
rotkip
rotkipOP2y ago
I'll certainly do and thx for responding @IceGPT
FusedQyou
FusedQyou2y ago
Not a Unity specific issue at all The problem is that you try to call instance before it has been assigned. My guess is you never assigned it at all, because you don't have the component anywhere in the scene You assign this in Awake, and this method only calls for the component instance if it actually has an instance, which requires it to exist in the scene. So I would check if you actually put it somewhere Other than that the code looks nice, although maybe the singleton implementation you have currently could be improved. 👍
rotkip
rotkipOP2y ago
Am at work right now, but I’ll definitely look into it when I’m home I'm pretty new to Unity and C#, so the instance gets destroyed when I switch scenes? Should I just make 2 scenes or 2 Highscore game objects, 1 in Game and 1 in Leaderboard? I did this and I noticed that I never let the player set a username and that's why the score wasn't uploaded properly, but I can't find any documentation that I understand on how to share the data of variables over scenes. Because I need to send the username data from the menu scene to the game scene, do you have an idea on how to do this?
FusedQyou
FusedQyou2y ago
It does get destroyed If you don't want it to get destroyed, call DontDestroyOnLoad and pass the object into it Idk how your game looks so I assume you have two because you had two scenes?
rotkip
rotkipOP2y ago
yes thx for responding so fast *3, one for the menu, one for the game and one for the leaderboard. but I'll only use the username variable for the first two
FusedQyou
FusedQyou2y ago
Yeah not much sense to have a singleton when you got 3 of them So you need to do that to make it one probably
rotkip
rotkipOP2y ago
Could you explain further?
FusedQyou
FusedQyou2y ago
Call DontDestroyOnLoad on the gameobject when you assign Instance
rotkip
rotkipOP2y ago
Thx for your help, but I thought that this was a small project that I could handel. I'll leave it alone until I understand more about Unity and C#. Still thx for the help
rotkip
rotkipOP2y ago
Here is some footage of the game
FusedQyou
FusedQyou2y ago
You do seem to have a nice grasp on how to work with unity, but edge cases like these can be unforgiving and you will learn them by experience Generally I would advice you learn c# first without Unity, by making a project with it. Personally I learned a lot of c# by making a Discord bot Feel free to ask more questions but remember that you learn the best way by actually using c# and trying out different stuff
rotkip
rotkipOP2y ago
thx, I’ll definitely follow your advice
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server