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!

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #19172
    soop
    Level 10
    Silver Supporter (Patron)
    Helpful?
    Up
    2
    ::

    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)];
                //#endif
    
    and 1 other person have upvoted this post.
    #19173
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    Thanks for flagging this soop. Really appreciate it. Will be looking to recode this piece of code so that it becomes more efficient.

    #19174
    soop
    Level 10
    Silver Supporter (Patron)
    Helpful?
    Up
    1
    ::

    You’re the best Terence :)

      1 anonymous person
    has upvoted this post.
    #19180
    giselle
    Level 4
    Moderator
    Helpful?
    Up
    0
    ::

    Hi soop! Here is a more efficient code that uses AssetDatabase.FindAssets with 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();
        }
Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: