Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved UnityEngine.AddressableAssets.InvalidKeyException on Build but not on Play

Discussion in 'Addressables' started by iLoveGamesDeveloper, May 21, 2023.

  1. iLoveGamesDeveloper

    iLoveGamesDeveloper

    Joined:
    Aug 8, 2019
    Posts:
    14
    Hello!

    I'm using addressables for a simple case which is a ScriptableObject singleton where its life doesn't depend on the script loading sequence. It works in Play fine, however, it doesn't work when I build the game. Looking at the log I am getting:


    UnityEngine.AddressableAssets.InvalidKeyException: Exception of type 'UnityEngine.AddressableAssets.InvalidKeyException' was thrown. No Location found for Key=InspectorConstantManager
    NullReferenceException: Object reference not set to an instance of an object


    I have tried New and Clean build but they have not worked.

    This was my workflow to create the addressable:
    • I created the ScriptableObject
    • Assigned it as addressable in the inspector (checkbox)
    • Created an addressable label that is the exact name as the asset
    • I did not change the grouping
    I then play in the editor and it works great, but when I build the game, it does not work. Anything you think I can do to debug this?

    I'm using 1.19.19 and Unity 2021.3.24f1.

    This is the singleton generic script:

    Code (CSharp):
    1. public class ScriptableObjectSingleton<T> : ScriptableObject where T : ScriptableObjectSingleton<T>
    2.     {
    3.         private static T instance;
    4.         public static T Instance
    5.         {
    6.             get
    7.             {
    8.                 if (instance == null)
    9.                 {
    10.                     var op = Addressables.LoadAssetAsync<T>(typeof(T).Name);
    11.  
    12.                     instance = op.WaitForCompletion();
    13.                 }
    14.                 return instance;
    15.             }
    16.         }
    17.     }
     
  2. iLoveGamesDeveloper

    iLoveGamesDeveloper

    Joined:
    Aug 8, 2019
    Posts:
    14
    I fixed this. There was an issue with the labeling name. I had to make sure the label, script, and object were exactly the same for this use case.