Unregister Inventory on Player destroy ?

emsi

New member
Hello, I've following issue:

I've Player object with InventoryIdentifier component. I need to destroy player object later in game and spawn a new one.
When new Player is spawned I've got this error:

InvalidCastException: Specified cast is not valid.
Opsive.UltimateInventorySystem.Core.Registers.InventoryObjectIDOnlyRegister`1[T].AssignNewID (T obj) (at Assets/Opsive/UltimateInventorySystem/Scripts/Core/Registers/InventoryObjectRegister.cs:180)
Opsive.UltimateInventorySystem.Core.Registers.InventoryObjectIDOnlyRegister`1[T].RegisterInternal (T obj) (at Assets/Opsive/UltimateInventorySystem/Scripts/Core/Registers/InventoryObjectRegister.cs:243)
Opsive.UltimateInventorySystem.Core.Registers.InventoryObjectIDOnlyRegister`1[T].Register (T obj) (at Assets/Opsive/UltimateInventorySystem/Scripts/Core/Registers/InventoryObjectRegister.cs:225)
Opsive.UltimateInventorySystem.Core.InventoryCollections.InventoryIdentifier.Awake () (at Assets/Opsive/UltimateInventorySystem/Scripts/Core/InventoryCollections/InventoryIdentifier.cs:55)
UnityEngine.Object:Instantiate(Character, Vector3, Quaternion)

Probably I need to somehow unregister it before, but I am not sure how. Thanks for help
 
What UIS version are you using, the current version is v1.1.3?
I have checked the error line and it doesn't match up to anything in my project... so this might have already been fixed.

Unrelated to this error, but if you destroy the player without destroying the UI you will also have issues in v1.1.3.

This should all be fixed in the coming version 1.1.4 (planned for next week)
 
Some people have experienced issues when updating the asset using the asset manager so make sure to double check the version in the UIS Editor
1611143414813.png

If you really have v1.1.3 installed it should work... but if for some reason it does not, then please replace those two functions in the "InventoryObjectRegister.cs" script:


Code:
/// <summary>
/// Assigns a new ID to the object.
/// </summary>
/// <param name="obj">The object.</param>
internal void AssignNewID(T obj)
{
    if (!(obj is IObjectWithID objWithID)) {
        Debug.LogWarning($"Dupplicate ID '{obj.ID}' found, a new ID cannot be auto generated for: " + obj);
        return;
    }
    var newID = RandomID.Generate();
    while (IDIsAvailable(newID) == false) { newID = RandomID.Generate(); }
    objWithID.ID = newID;
}

/// <summary>
/// Registers the Object.
/// </summary>
/// <param name="obj">The object.</param>
/// <returns>True if the object was registered.</returns>
protected virtual bool RegisterInternal(T obj)
{
    if (RegisterConditions(obj) == false) { return false; }
    if (IsIDEmpty(obj)) { AssignNewID(obj); }
    var addToDictionaries = false;
    if (m_DictionaryByID.TryGetValue(obj.ID, out var registered)) {
        if (registered == null || registered.Equals(null)) {
            m_DictionaryByID.Remove(obj.ID);
            addToDictionaries = true;
        } else if (ReferenceEquals(registered, obj) == false) {
            AssignNewID(obj);
            addToDictionaries = true;
        }
    } else {
        addToDictionaries = true;
    }
    if (addToDictionaries) {
        AddInternal(obj);
    }
    return true;
}
 
you're right, i didnt have version 1.1.3 when check with UIS editor. but in package manager it said it's current. thanks
 
Top