0
0
Unityframework~30 mins

Variables and serialization in Unity - Mini Project: Build & Apply

Choose your learning style9 modes available
Variables and Serialization in Unity
📖 Scenario: You are creating a simple Unity game where you want to save the player's name and score so you can load it later.
🎯 Goal: Learn how to create variables and use serialization to save and load player data in Unity.
📋 What You'll Learn
Create variables to store player name and score
Use Unity's serialization to save player data
Load and display saved player data
💡 Why This Matters
🌍 Real World
Saving player data is essential in games to keep progress between sessions.
💼 Career
Understanding variables and serialization is key for game developers working with Unity to manage game state and data persistence.
Progress0 / 4 steps
1
Create player data variables
Create a public class called PlayerData with two public variables: a string called playerName and an int called playerScore.
Unity
Need a hint?

Use public string playerName; and public int playerScore; inside the class.

2
Add serialization attribute
Add the [System.Serializable] attribute above the PlayerData class to enable Unity to serialize it.
Unity
Need a hint?

Place [System.Serializable] right before public class PlayerData.

3
Create and assign player data instance
In a new GameManager class, create a public variable called playerData of type PlayerData. Then, in the Start() method, first instantiate it with playerData = new PlayerData();, then assign playerData.playerName to "Alex" and playerData.playerScore to 100.
Unity
Need a hint?

Declare public PlayerData playerData;, instantiate with playerData = new PlayerData(); and assign values inside Start().

4
Print player data to console
In the Start() method of GameManager, add a Debug.Log statement to print the player's name and score in the format: "Player: Alex, Score: 100".
Unity
Need a hint?

Use Debug.Log($"Player: {playerData.playerName}, Score: {playerData.playerScore}"); inside Start().