Okay, so today I decided to mess around with game state, you know, like saving progress and stuff. I’ve always been kinda intimidated by it, felt like a whole can of worms I didn’t wanna open. But, hey, gotta learn sometime, right?

First Steps – What even IS State?
First, I had to figure out what I even wanted to save. My little test game is super basic – just a player moving around and collecting coins. So, the obvious things were:
- Player position
- Number of coins collected
- Maybe some level progress, if I add more levels later
I started by just focusing on the player position. I figured if I can save and load that, I can save anything.
The “Aha!” Moment (and Many Fails)
My first try? I just threw some variables at the top of my script and hoped for the best. Yeah, that didn’t work. When I restarted the game, everything went back to zero. Duh.
Then, I started digging around online, found some stuff about PlayerPrefs. Sounded promising! It’s like a super simple way to store little bits of data. I used to save the player’s x and y coordinates separately.
So, in the part of the code where my player moves, i save the x,y positoin using to store player’s X,Y coordinates separately.
I was stoked! I hit play, moved my character, stopped the game, and hit play again…and…nothing. Still back at the starting point. Turns out, I was saving the data, but I wasn’t loading it anywhere!
Loading it Up – Finally Getting Somewhere
I added some more code, this time using , in the game’s start function. This grabs the saved values (if they exist) and sets the player’s position to those values. I slapped some * in the code and play the game, and It’s work now! The player position get loaded.
I added a few lines to save and load the coin count, using and . Worked like a charm!

What I Learned (and What’s Next)
Okay, so today’s experiment was pretty basic, but I learned a ton:
- Saving game state isn’t as scary as I thought.
- PlayerPrefs is super handy for simple stuff.
- I need to be way more organized with my code. It’s getting messy!
- Saving a simple string value by and .
Next time, I wanna try saving more complex data. Maybe a whole inventory of items? Or maybe I’ll look into other saving methods, like writing to actual files. Baby steps, though. For now, I’m just happy I can finally save my game!