First Name
Last Name
Confirm Password
Please validate your account by clicking the link in your email
Resend Validation Email

Making Snake in Unity: Sound Highscore


After all this time with our game on mute lets add some sounds!
Were also going to use Player Prefs to save our highscore.

Download Project Files
Post Comment


Thank for teaching
But what if i want to make a leaderboard instead of high score
please teach us
Code Monkey
Check out the Highscore Table video https://www.youtube.com/watch?v=iAbaqGYdnyI
Unzerstört HD
Sometimes I cant see the body or the Face Can you help me?
Unzerstört HD
I have Problems the scene couldn`t change

Scene 'Loading' couldn't be loaded because it has not been added to the build settings or the AssetBundle has not been loaded.
To add a scene to the build settings use the menu File->Build Settings...
Loader:Load(Scene) (at Assets/Scripts/Loader.cs:37)
<>c:<Awake>b__1_0() (at Assets/Scripts/MainMenuWindow.cs:29)
CodeMonkey.Utils.Button_UI:OnPointerClick(PointerEventData) (at Assets/CodeMonkey/Utils/Button_UI.cs:95)
Code Monkey
Open the Build settings and make sure all scenes are there, just drag them.
Hey CodeMonkey, thanks for this tutorial! It was very helpful. I had a question - instead of reloading the scene on Game Over, is there a way that we can delete/store the body parts when dealing with game over? I ask because I'm trying to train an ML agent to play the game, but I need to be able to reset the environment without actually restarting the scene. I've tried calling a simple for-loop that iterates through the snakeBodyList and destroys the game object, but that gives a MissingReferenceException in the SetSnakeMovePosition() function. Any ideas?
Code Monkey
You have to manually destroy all the snake body parts, reset the score, all the stats, etc. Loading the same thing is the easy way but it can certainly be done manually, just look at what stats you need to manually reset.
Check your stack trace on that error, where is that function being called? Make sure you reset the snakeBodyList so you don't have it filled with destroyed references.
@Code Monkey Yep, earlier this morning I finally realized that it was all because I wasn't clearing the snakeBodyList. facepalm. after changing that I've now got the agent training normally. Now to see if I can get it to actually play the game! Thank you for the reply, and keep up the tutorials!
Bram Van Wambeke
hey CodeMonkey,
Aafter making the game alongside you i noticed that if you press the keys fast enough you can get the snake to go backwards(first snake moves left, Then you press uparrow and right arrow, and the snake will move "backwards" causing the snake to die if u have more then one body piece.
I fixed the problem by adding a boolean thats set to true in the if statement in handlegridMovement and having it check if its true in the if (Input.GetKeyDown(KeyCode.UpArrow) if.
(If(Input.GetKeyDown(KeyCode.UpArrow) && RefreshMovement == true)),
And then setting it to false in the if where you change the direction, (So that you can only change your direction one time).
Im sorry if somebody else already pointed this out but i couldnt find any other comments on this and i was wondering if there was a more efficient way of solving this problem. (Sorry for my bad english <3, Love your channel u are a big inspiration to me).
Code Monkey
Oh yeah I remember seeing that bug and apparently completely forgot to fix it.
Yeah the issue was as you said if you quickly changed the last move direction but didn't have time to actually move. Your solution seems fine, all you need is to make sure the direction cannot change more than once before the snake moves again.
I still would like to do one more video on this polishing various aspects of the game so Ill have to remember to fix that issue when I do.
Wisdom Allen
The script to spawn "Ding!" popup message didn't popup. I got "the referenced script on this behavior is missing". Do I have to import all assets because I only imported the codemonkey folder since I want to practice fully.
Code Monkey
The package contains all the assets and code so if you didn't import it fully then theres something you're missing. Don't know what since I don't know what you didn't import.
Import everything onto an empty scene and see the differences.
Dante Honorato
Hey! I really love your videos buut I hae a question.. I want to make a highscore but with best time. And I want to use the clock UI and script you showed in another video. How do I do it?
Code Monkey
If you're using a float to count up the number of seconds you would just store that. I've done a video on a Highscore table in which I saved int's but it would be simple to convert it to saving floats and then reverse the sorting to go from lowest time to highest.

The ClockUI just displays the time, how you display it isn't relevant to how you store it.
hater boy
What programming language you use?
Code Monkey
This is written in C#
Hey, Where can i download the snake body and head files?
Code Monkey
They are included in the project files that you can grab from the website
Another great video, thanks, a quick video on how to change to Android would be great!
Code Monkey
The game has a pretty simple input with just the arrow keys so it would be simple to add on screen keys and everything else should work. I'll look into it, cheers!
Ahmed Samy
In your suggestions you have a create A top down racing game And The top down GTA I can't wait to see there
Your utilities Files Is very helpful When you make a big game
Code Monkey
Glad you find the utilities useful!
Ahmed Samy
Very nice,
Please what is the name of the program you use to create The audio effects
And I have a question: why are you don't use unity bilt en functions like on trigger enter on trigger exit Instead of that you make everything from scratch
Code Monkey
Its ChipTone http://sfbgames.com/chiptone/
Because that would make it needlessly complex. The game is played on a Grid so there's no reason to mess around with Physics and heavy geometry calculations.
It's much simpler to just test one grid position if its matches another.
But yes that would work just fine, put a Rigidbody in the head and BoxColliders in the rest of the body and Food.
Ahmed Samy
that's true thank you a lot
Kinshi GameS
First Dislike is mine —__—
Ahmed Samy
Why he is very awesome man and he makes a Professional tutorials for free
Kinshi GameS
Yes but no one puts a dislike, and im the first but not on likes so I dislike it
Code Monkey
Code Monkey
Dislikes hurt the video and hurt the channel, why would you do that just to be the first dislike?
Kinshi GameS
Okay i”ll like the vid , just for u
عباس البصري
Thank you for our teaching.