UCC Character Prefab error on Instantiate after 3rd character with inventory - StateManager.InitializeState()

bbjones

Member
Doing some basic spawn testing related to the script in this post: https://www.opsive.com/forum/index....th-different-item-inventories.1948/#post-9777

I can spawn in a UCC character prefab with inventory twice, but on the third attempt I get an error to do with StateManager.InitializeState().

The full code is in the post link above, but boils down to these two snippets:

Spawn in the first character and assign inventory using:
C#:
        spawnedCharacter = GameObject.Instantiate(characterDefaultKits[c].CharacterPrefab);
...
characterDefaultKits[c].ItemPickups[i].DoItemPickup(spawnedCharacter, inventory, -1, true, true);

Works fine.

Spawn in the second character, which first destroys the previous one:
C#:
         if (spawnedCharacter != null)
        {
            Destroy(spawnedCharacter);
        }

Then instantiates the same way as above. Works fine.

Spawn in a third character, which first destroys the previous one and then gets an error calling GameObject.Instantiate(...), which initializes the UCC character sending it through the StateManager ending up in an error. Several objects are Initialized this way and I can't see why things change when spawning in the third character.
StateManagerInitializeIssue.png


ArgumentException: An item with the same key has already been added. Key: Default
System.Collections.Generic.Dictionary`2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) (at <599589bf4ce248909b8a14cbe4a2034e>:0)
System.Collections.Generic.Dictionary`2[TKey,TValue].Add (TKey key, TValue value) (at <599589bf4ce248909b8a14cbe4a2034e>:0)
Opsive.UltimateCharacterController.StateSystem.StateManager.InitializeInternal (UnityEngine.GameObject gameObject, Opsive.UltimateCharacterController.StateSystem.IStateOwner owner, Opsive.UltimateCharacterController.StateSystem.State[] states) (at Assets/Opsive/UltimateCharacterController/Scripts/StateSystem/StateManager.cs:105)
Opsive.UltimateCharacterController.StateSystem.StateManager.Initialize (UnityEngine.GameObject gameObject, Opsive.UltimateCharacterController.StateSystem.IStateOwner owner, Opsive.UltimateCharacterController.StateSystem.State[] states) (at Assets/Opsive/UltimateCharacterController/Scripts/StateSystem/StateManager.cs:67)
Opsive.UltimateCharacterController.StateSystem.StateObject.Initialize (UnityEngine.GameObject gameObject) (at Assets/Opsive/UltimateCharacterController/Scripts/StateSystem/StateObject.cs:29)
Opsive.UltimateCharacterController.Inventory.ItemSet.Initialize (UnityEngine.GameObject gameObject, Opsive.UltimateCharacterController.Inventory.ItemSetManager itemSetManager, System.Int32 categoryID, System.Int32 categoryIndex, System.Int32 index) (at Assets/Opsive/UltimateCharacterController/Scripts/Inventory/ItemSet.cs:84)
Opsive.UltimateCharacterController.Inventory.ItemSetManager.AddItemSet (Opsive.UltimateCharacterController.Items.Item item, Opsive.UltimateCharacterController.Inventory.ItemSet itemSet, System.Boolean defaultItemSet) (at Assets/Opsive/UltimateCharacterController/Scripts/Inventory/ItemSetManager.cs:184)
Opsive.UltimateCharacterController.Objects.CharacterAssist.ItemPickup.DoItemPickup (UnityEngine.GameObject character, Opsive.UltimateCharacterController.Inventory.InventoryBase inventory, System.Int32 slotID, System.Boolean immediatePickup, System.Boolean pickupItemType) (at Assets/Opsive/UltimateCharacterController/Scripts/Objects/CharacterAssist/ItemPickup.cs:171)
SpawnCharacter.SpawnACharacter () (at Assets/_PolySpawnWithItems/SpawnCharacter.cs:147)

(... I didn't include the stack trace prior to this as it is just about me clicking a UI button to fire off the SpawnACharacter() method)

Important to note, this error will only occur if I spawn in 3 characters that had inventory using DoItemPickup().
I can spawn in characters with no inventory without error. But once you spawn in a third character on top of ones destroyed that had inventory, I get the error.

For example:
  • I can spawn characters with no inventory all day without error
  • spawn 1 or 2 characters with inventory, the rest without, no error, regardless of what order I spawn them in
  • spawn any mix of characters in any order with and without inventory without error, until trying to spawn in the 3rd character with inventory - causes the error
From a gameplay scenario perspective, you could think of it like this:
  • There are several pre-made characters with default inventories to choose from to play, but each only gets one life
  • Select and spawn the 1st character to play, it dies
  • Select and spawn the 2nd character to play, it dies
  • Select and spawn the 3rd character to play, get the error above
I'm going to move on to spawning characters using the more complete approach based on the UMA integration using CharacterBuilder.
 
After more help from Justin sorting out the repro package via email, this issue was resolved.

Justin provided some updated UCC scripts to fix the issue.

A complete test is demonstrated here including
 
Top