Fix NullReferenceException crashes when asset bundle is missing
Problem: When the 'serverbrowserpkg' asset bundle file is missing, the mod crashed at multiple points: - PrefabManager.PreScriptLoad(): NullReferenceException on line 58 - ServerBrowser.SceneLoaded(): "Object to instantiate is null" at line 312 - Main.TransitionTo(): NullReferenceException at lines 227-228 These crashes made the mod unusable and prevented proper error reporting. Root cause: The asset bundle file containing UI prefabs was missing from the mod directory, causing all prefab references to be null. The code didn't check for null before using these references. Solutions: PrefabManager.cs: - Add null check after LoadAssetBundle() call (line 31-36) - Return early with clear error message if bundle is null - Provide guidance to user about missing file - Better logging for successful loads ServerBrowser.SceneLoaded(): - Add prefab null check at method start (line 302-309) - Return early if prefabs are null - Prevents crash during UI instantiation - Clear error messages in log Main.TransitionTo(): - Add comprehensive null checks before using UI references (line 228) - If UI not loaded but user tries to access multiplayer menu: * Show user-friendly modal dialog * Explain the problem clearly * Provide reinstall guidance - Gracefully handle missing UI without crashing Results: ✅ No crashes when asset bundle is missing ✅ Clear, actionable error messages for users ✅ Graceful degradation - rest of mod still works ✅ User gets helpful modal instead of silent crash ✅ Better debugging with detailed logs Updated README.md with: - Documentation of all asset bundle fixes - Code examples showing the changes - Known Issues section noting missing asset bundle - Instructions for resolving the issue 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -28,12 +28,19 @@ namespace KCM
|
||||
|
||||
assetBundle = KCModHelper.LoadAssetBundle(_helper.modPath, "serverbrowserpkg");
|
||||
|
||||
Main.helper.Log(String.Join(", ", assetBundle.GetAllAssetNames()));
|
||||
if (assetBundle == null)
|
||||
{
|
||||
Main.helper.Log("ERROR: Asset bundle 'serverbrowserpkg' not found! UI features will not work.");
|
||||
Main.helper.Log("Please ensure the asset bundle file is in the mod directory.");
|
||||
return;
|
||||
}
|
||||
|
||||
Main.helper.Log("Asset bundle loaded successfully");
|
||||
Main.helper.Log("Assets in bundle: " + String.Join(", ", assetBundle.GetAllAssetNames()));
|
||||
|
||||
serverBrowserPrefab = assetBundle.LoadAsset("assets/workspace/serverbrowser.prefab") as GameObject;
|
||||
serverEntryItemPrefab = assetBundle.LoadAsset("assets/workspace/serverentryitem.prefab") as GameObject;
|
||||
|
||||
|
||||
serverLobbyPrefab = assetBundle.LoadAsset("assets/workspace/serverlobby.prefab") as GameObject;
|
||||
serverLobbyPlayerEntryPrefab = assetBundle.LoadAsset("assets/workspace/serverlobbyplayerentry.prefab") as GameObject;
|
||||
serverChatEntryPrefab = assetBundle.LoadAsset("assets/workspace/serverchatentry.prefab") as GameObject;
|
||||
@@ -41,10 +48,11 @@ namespace KCM
|
||||
|
||||
modalUIPrefab = assetBundle.LoadAsset("assets/workspace/modalui.prefab") as GameObject;
|
||||
|
||||
Main.helper.Log("Loaded assets");
|
||||
Main.helper.Log("Loaded all UI prefabs successfully");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Main.helper.Log("ERROR loading asset bundle:");
|
||||
Main.helper.Log(ex.ToString());
|
||||
Main.helper.Log(ex.Message);
|
||||
Main.helper.Log(ex.StackTrace);
|
||||
|
||||
Reference in New Issue
Block a user