Unity Learn home
0
0
Data Persistence
46 views
Goal: Persist player name and best score between scenes/sessions; show name + score in Start Menu and Main scene.

Files Edited:

DataHolder.cs
MainManager.cs
StartMenu.cs
DataHolder.cs (what I added):

Singleton: public static DataHolder Instance { get; private set; } and DontDestroyOnLoad in Awake() so the object persists across scenes.
Fields: playerName, bestScore, bestPlayer, POINTS.
Persistence methods: Save() / Load() using PlayerPrefs for BestScore and BestPlayer.
SetPoint(int): updates POINTS, compares with bestScore, updates bestPlayer and calls Save() when a new best is reached.
Accessors: SetPlayerName(string), GetPlayerName(), GetBestScore(), GetBestPlayer().
Debugging: added logs inside SetPoint to show incoming points, current best, and whether a new best was saved.
MainManager.cs (what I added/changed):

Call on GameOver: DataHolder.Instance.SetPoint(m_Points) to notify DataHolder when a run ends.
Fallback fix: replaced an unsafe fallback that accessed DataHolder.Instan
Media
1
1