Forum begins after the advertisement:
[Part 16] WARNING – Delete this CharacterSelector code!
Home › Forums › Video Game Tutorial Series › Creating a Rogue-like Shoot-em Up in Unity › [Part 16] WARNING – Delete this CharacterSelector code!
- This topic has 4 replies, 3 voices, and was last updated 1 hour, 39 minutes ago by
Terence.
-
AuthorPosts
-
January 3, 2026 at 4:24 pm #19172::
In one of the episodes, we add this block that is supposed to remedy the NullReferenceException for CharacterData if you do not go through the character select scene. It is EXTREMELY bad for large projects as (I believe) it searches through EVERY asset. Learned this the hard way after importing an asset library, and dealing with 30s+ freezes on Play.
A potential fix would be to create a “Character Database” scriptable object, and just pick one randomly from there.
//// Randomly pick a character if we are playing from the Editor. //#if UNITY_EDITOR //string[] allAssetPaths = AssetDatabase.GetAllAssetPaths(); //List<CharacterData> characters = new List<CharacterData>(); //foreach (string assetPath in allAssetPaths) //{ // if (assetPath.EndsWith(".asset")) // { // CharacterData characterData = AssetDatabase.LoadAssetAtPath<CharacterData>(assetPath); // if (characterData != null) // { // characters.Add(characterData); // } // } //} //// Pick a random character if we have found any characters. //if (characters.Count > 0) return characters[Random.Range(0, characters.Count)]; //#endifand 1 other person have upvoted this post. January 4, 2026 at 3:36 pm #19173::Thanks for flagging this soop. Really appreciate it. Will be looking to recode this piece of code so that it becomes more efficient.
January 4, 2026 at 6:13 pm #19174January 6, 2026 at 10:50 pm #19180::Hi soop! Here is a more efficient code that uses
AssetDatabase.FindAssetswith a type filter and a specific ‘Assets’ folder path, letting Unity quickly find and load the CharacterData asset. Let me know if it works for you!public static CharacterData[] GetAllCharacterDataAssets() { List<CharacterData> characters = new List<CharacterData>(); // Populate the list with all CharacterData assets (Editor only). #if UNITY_EDITOR string[] guids = AssetDatabase.FindAssets("t:CharacterData", new[] { "Assets" }); if (guids.Length > 0) { string randomGuid = guids[Random.Range(0, guids.Length)]; string assetPath = AssetDatabase.GUIDToAssetPath(randomGuid); CharacterData randomCharacter = AssetDatabase.LoadAssetAtPath<CharacterData>(assetPath); return new CharacterData[] { randomCharacter }; } #else Debug.LogWarning("This function cannot be called on builds."); #endif return characters.ToArray(); }February 1, 2026 at 11:43 pm #19245::There were some small mistakes with @giselle’s codes above:
public static CharacterData[] GetAllCharacterDataAssets() { List<CharacterData> characters = new List<CharacterData>(); // Populate the list with all CharacterData assets (Editor only).if UNITY_EDITOR
string[] guids = AssetDatabase.FindAssets("t:CharacterData", new[] { "Assets" });if (guids.Length > 0) { string randomGuid = guids[Random.Range(0, guids.Length)]; string assetPath = AssetDatabase.GUIDToAssetPath(randomGuid); CharacterData randomCharacter = AssetDatabase.LoadAssetAtPath<CharacterData>(assetPath); return new CharacterData[] { randomCharacter }; }foreach (string s in guids) { string assetPath = AssetDatabase.GUIDToAssetPath(s); CharacterData cData = AssetDatabase.LoadAssetAtPath<CharacterData>(assetPath); if(cData) characters.Add(cData); }else
Debug.LogWarning("This function cannot be called on builds.");endif
return characters.ToArray(); }She accidentally changed the code to retrieve a random character, instead of assembling a list of all characters.
-
AuthorPosts
- You must be logged in to reply to this topic.
Advertisement below: