Search code examples
c#unity-game-engineresourcesassets

Resources.Load fails in some cases but runs correct in other cases


Maybe I am not seeing the issue after 2 hours of debugging now. I have the following code for finding a Name in an Enum and loading the asset in an object:

public PokemonBase FindPokemonName(string ChosenPokemon)
{
    try
    {
        PokedexNames NameIndex = (PokedexNames)Enum.Parse(typeof(PokedexNames), ChosenPokemon);
        pBase = Resources.Load("Assets/Pokemon/" + Convert.ToInt32(NameIndex), typeof(PokemonBase)) as PokemonBase; // --> Fehlerhaft? Lädt im DebuggingTool, aber nicht in TwitchCommands.
        #if UNITY_EDITOR
        Debug.Log("Index: " + Convert.ToInt32(NameIndex) + ". Pokemon Name vom Asset: " + pBase.Name);
        Debug.Log("Index aus PokemonBase?: " + pBase.name);
        #endif
        return pBase;
    }
    catch
    {
        pBase = Resources.Load("Assets/Pokemon/default", typeof(PokemonBase)) as PokemonBase; // --> Fehlerhaft? Lädt im DebuggingTool, aber nicht in TwitchCommands.
        #if UNITY_EDITOR
        Debug.Log("hat nicht geklappt");
        #endif
        return pBase;
    }
} 

This code is running good with my own "Debuggingtool". I call the method with a valid value and get a positive result.

My problem is that the code will fail to "frame not in module" when I call the method from another file:

Debug.Log("Got Whisper");
if (ChatMessage.Length > 6) {
    string[] MessageSplitForPokemonName = ChatMessage.Split(' '); //Creates an Array, [ "!werde", "Glumanda"]
    pBase = PKFunc.FindPokemonName(MessageSplitForPokemonName[1]); 

All objects in Unity are declared and no missing objects are visible. I crossed out unnecessary objects in the images.

With DebuggingTool (Debug Logs are OK here)

With the normal method (After "Got whisper" nothing is displayed bc Loading fails)

What am I doing wrong?


Solution

  • I don't know how DebuggingTool or TwitchCommands works in detail but in general

    Don't use Resources in most of cases!


    That said for being able to use Resources.Load it would require those assets to be placed inside a folder called Resources which during the build is packed into the APK.

    The path then should also not include the full path but only whatever is the path relative to that Resources folder.

    The path does not need to include Assets and Resources in the string, for example loading a GameObject at Assets/Guns/Resources/ Shotgun.prefab would only require Shotgun as the path. Also, if Assets/Resources/Guns/Missiles/PlasmaGun.prefab exists it can be loaded using Guns/Missiles/PlasmaGun as the path string.


    However the frame not in module could also just come from your code being called from a background thread/task. Most of Unity API can only be used on the Unity "main" thread so if this ChatMessage is being received asynchronously you have to dispatch the further processing that touches the Unity API back into the main thread (see e.g. UnityMainThreadDispatcher)