Skip to content

Commit 6b78906

Browse files
committed
refactor: improve null-safety, remove unsafe field initializers, and clean up achievement checks
- CarMove.cs: Added null checks for _pgsController before checking sign-in status or updating leaderboards to prevent potential NullReferenceExceptions. Simplified duplicated _driveAchievementUnlocked assignments by extracting them from the conditional branches. - GameData.cs: Moved unsafe Unity API calls (FindFirstObjectByType and GameObject.Find) out of the field initializers in the serializable GameData class to prevent exceptions during JSON deserialization. Implemented lazy on-demand initialization for PlayerController and background images, and removed a redundant .gameObject call. - FriendsPageController.cs: Added null checks for localUser and localUser.friends before iterating the friends list. Replaced explicit array initializers to avoid referencing the deprecated IUserProfile type, resolving the compiler warning.
1 parent bb3b91e commit 6b78906

3 files changed

Lines changed: 37 additions & 20 deletions

File tree

trivialkart/trivialkart-unity/Assets/Scripts/Controller/Game/CarMove.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,23 +100,20 @@ private void FixedUpdate()
100100
#if PLAY_GAMES_SERVICES
101101
private void CheckDistanceAchievement(float newDistance)
102102
{
103-
if (newDistance >= ACHIEVEMENT_DRIVE_DISTANCE &&
103+
if (_pgsController != null &&
104+
newDistance >= ACHIEVEMENT_DRIVE_DISTANCE &&
104105
_pgsController.CurrentSignInStatus == PGSController.PgsSigninStatus.PgsSigninLoggedIn)
105106
{
106107
var achievementManager = _pgsController.AchievementManager;
107108
if (achievementManager != null)
108109
{
109-
if (achievementManager.GetAchievementUnlocked(
110+
if (!achievementManager.GetAchievementUnlocked(
110111
PGSAchievementManager.TrivialKartAchievements.Tk_Achievement_Drive))
111-
{
112-
_driveAchievementUnlocked = true;
113-
}
114-
else
115112
{
116113
achievementManager.UnlockAchievement(
117114
PGSAchievementManager.TrivialKartAchievements.Tk_Achievement_Drive);
118-
_driveAchievementUnlocked = true;
119115
}
116+
_driveAchievementUnlocked = true;
120117
}
121118
}
122119
}
@@ -126,7 +123,10 @@ private void Update()
126123
{
127124
#if PLAY_GAMES_SERVICES
128125
// Checks to see if we are due to update the distance traveled leaderboard
129-
_pgsController.UpdateLeaderboard();
126+
if (_pgsController != null)
127+
{
128+
_pgsController.UpdateLeaderboard();
129+
}
130130
#endif
131131
// Use keys to control menu/gameplay
132132
if (_garageAction.WasPressedThisFrame())

trivialkart/trivialkart-unity/Assets/Scripts/Controller/PGS/FriendsPageController.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,20 @@ private void LoadAdditionalFriends()
178178
// the string listing all the user's friends, comma-delimited
179179
private void AddFriendsFromList()
180180
{
181-
foreach (var friend in PlayGamesPlatform.Instance.localUser.friends)
181+
var localUser = PlayGamesPlatform.Instance.localUser;
182+
if (localUser != null && localUser.friends != null)
182183
{
183-
if (string.IsNullOrEmpty(_friendsList))
184+
foreach (var friend in localUser.friends)
184185
{
185-
_friendsList = friend.userName;
186-
}
187-
else
188-
{
189-
_friendsList += (", " + friend.userName);
186+
if (friend == null) continue;
187+
if (string.IsNullOrEmpty(_friendsList))
188+
{
189+
_friendsList = friend.userName;
190+
}
191+
else
192+
{
193+
_friendsList += (", " + friend.userName);
194+
}
190195
}
191196
}
192197
}

trivialkart/trivialkart-unity/Assets/Scripts/Objects/GameData.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ public class GameData
6262
private const int InitialCoinAmount = 20;
6363
private const int TotalCarCount = 4;
6464
private const int TotalBackgroundCount = 2;
65-
private PlayerController _playerController = Object.FindFirstObjectByType<PlayerController>();
66-
private GameObject _backgroundImages = GameObject.Find("Background/backGroundImages").gameObject;
65+
private PlayerController _playerController;
66+
private GameObject _backgroundImages;
6767

6868

6969
public GameData()
@@ -186,7 +186,11 @@ public bool CheckCarOwnership(CarList.Car car)
186186
public void UpdateCarInUse(CarList.Car targetCar)
187187
{
188188
carInUseName = targetCar.Name;
189-
_playerController.UpdateCarInUse();
189+
if (_playerController == null)
190+
{
191+
_playerController = Object.FindFirstObjectByType<PlayerController>();
192+
}
193+
_playerController?.UpdateCarInUse();
190194
}
191195

192196
// Check if the user owns a specific background.
@@ -201,9 +205,17 @@ public void UpdateBackgroundInUse(BackgroundList.Background targetBackground)
201205
{
202206
backgroundInUseName = targetBackground.Name;
203207

204-
foreach (Transform background in _backgroundImages.transform)
208+
if (_backgroundImages == null)
205209
{
206-
background.gameObject.GetComponent<SpriteRenderer>().sprite = targetBackground.ImageSprite;
210+
_backgroundImages = GameObject.Find("Background/backGroundImages");
211+
}
212+
213+
if (_backgroundImages != null)
214+
{
215+
foreach (Transform background in _backgroundImages.transform)
216+
{
217+
background.gameObject.GetComponent<SpriteRenderer>().sprite = targetBackground.ImageSprite;
218+
}
207219
}
208220
}
209221

0 commit comments

Comments
 (0)