0
0
Unityframework~20 mins

Public vs SerializeField in Unity - Hands-On Comparison

Choose your learning style9 modes available
Understanding Public vs SerializeField in Unity
📖 Scenario: You are making a simple Unity game where you want to control a character's speed. You want to learn how to make variables visible and editable in the Unity Inspector while keeping your code clean and safe.
🎯 Goal: Learn the difference between public and [SerializeField] in Unity by creating a speed variable that you can edit in the Inspector but keep private in code.
📋 What You'll Learn
Create a private float variable called speed with the value 5.0f.
Add the [SerializeField] attribute to the speed variable.
Create a public float variable called jumpHeight with the value 2.0f.
Print both speed and jumpHeight values in the Start() method.
💡 Why This Matters
🌍 Real World
Game developers often need to adjust variables like speed or jump height in the Unity Editor without changing code. Using <code>public</code> or <code>[SerializeField]</code> helps with this.
💼 Career
Understanding how to control variable visibility and editing in Unity is essential for game programming jobs, enabling collaboration between programmers and designers.
Progress0 / 4 steps
1
Create private speed variable with SerializeField
Create a private float variable called speed and set it to 5.0f. Add the [SerializeField] attribute above it.
Unity
Need a hint?

Use [SerializeField] just above the private variable declaration.

2
Add public jumpHeight variable
Add a public float variable called jumpHeight and set it to 2.0f inside the PlayerController class.
Unity
Need a hint?

Declare the variable with the public keyword and assign 2.0f.

3
Print speed and jumpHeight in Start method
Create a Start() method and inside it, use Debug.Log to print the values of speed and jumpHeight.
Unity
Need a hint?

Use void Start() and inside it call Debug.Log twice.

4
Run and observe output
Run the Unity game and observe the Console output showing the values of speed and jumpHeight. Write a Debug.Log statement that prints "Speed: 5" and "Jump Height: 2".
Unity
Need a hint?

Look at the Unity Console window after running the game to see the printed values.