Issues when dynamically regenerating a character

saboehnke

New member
Hey there. I am having an issue when attempting to dynamically regenerate a character with the 3rd person controller associated with it. What I mean by "regenerate" is I initially dynamically create the character with no issues. I have networked sessions in which host migrations require me to reconnect network objects appropriately. So...once I have the new network object (from FishNet) I move the child of the old network object (the child with the model in it) over to the new network object parent. I remove the Opsive components from the model and then attempt to rebuild the controller on the gameobject again. You can see in the error below that I am simply calling the CharacterBuilder.BuildCharacter() function with my parent GameObject and I pass the playerObj which is the character model itself (child of the parent). I have been digging into this for quite a while and I have found that in the InitializeInternal() of StateManager.cs it attempts to get the characterLocomotion from the stateGameObject using GetCachedParentComponent(). This returns "null" (including the quotes...so it is a string type). That causes it to go into the if statement that assigns the characterGameObject to characterLocomotion.GameObject...which doesn't exist. So...is the issue that the ICharacter instance is being cached for the original version of the character, and this causes the GetCachedParentComponent() to return a strange result? I'm not sure if that is the issue or not...but because that error occurs it stops the initialization in the AwakeInternal() of the AnimatorMonitor.cs as well which causes a lot of errors since it stops the m_CharacterLocomotion, etc. from being set.

Code:
MissingReferenceException: The object of type 'UltimateCharacterLocomotion' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotion.get_GameObject () (at Assets/External/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotion.cs:75)
Opsive.Shared.StateSystem.StateManager.InitializeInternal (UnityEngine.GameObject stateGameObject, Opsive.Shared.StateSystem.IStateOwner owner, Opsive.Shared.StateSystem.State[] states) (at Assets/External/Opsive/Shared/StateSystem/StateManager.cs:91)
Opsive.Shared.StateSystem.StateManager.Initialize (UnityEngine.GameObject gameObject, Opsive.Shared.StateSystem.IStateOwner owner, Opsive.Shared.StateSystem.State[] states) (at Assets/External/Opsive/Shared/StateSystem/StateManager.cs:64)
Opsive.Shared.StateSystem.StateBehavior.Awake () (at Assets/External/Opsive/Shared/StateSystem/StateBehavior.cs:27)
Opsive.UltimateCharacterController.Character.AnimatorMonitor.AwakeInternal () (at Assets/External/Opsive/UltimateCharacterController/Scripts/Character/AnimatorMonitor.cs:151)
Opsive.UltimateCharacterController.Utility.Builders.CharacterBuilder.AddEssentials (UnityEngine.GameObject character, UnityEngine.GameObject[] characterModels, System.Boolean addAnimator, UnityEngine.RuntimeAnimatorController[] animatorControllers, System.Boolean addPerspectiveMonitor, UnityEngine.Material invisibleShadowCasterMaterial, System.Boolean aiAgent, System.Boolean isNetworked) (at Assets/External/Opsive/UltimateCharacterController/Scripts/Utility/Builders/CharacterBuilder.cs:200)
Opsive.UltimateCharacterController.Utility.Builders.CharacterBuilder.BuildCharacter (UnityEngine.GameObject character, UnityEngine.GameObject[] characterModels, System.Boolean addAnimator, UnityEngine.RuntimeAnimatorController[] animatorControllers, System.String firstPersonMovementType, System.String thirdPersonMovementType, System.Boolean startFirstPersonPerspective, UnityEngine.GameObject[][] thirdPersonObjects, UnityEngine.Material invisibleShadowCasterMaterial, System.Boolean aiAgent) (at Assets/External/Opsive/UltimateCharacterController/Scripts/Utility/Builders/CharacterBuilder.cs:71)
PlayerNetworkBehavior.OnAvatarImportSuccess (UnityEngine.GameObject playerObj, DownloadCenter.Manifest.ManifestData manifest) (at Assets/FishNet/Scripts/PlayerNetworkBehavior.cs:154)
PlayerNetworkBehavior.RebuildCachedAvatar (UnityEngine.GameObject playerObj, DownloadCenter.Manifest.ManifestData manifest) (at Assets/FishNet/Scripts/PlayerNetworkBehavior.cs:219)
 
There is a lot going on and multiplayer is inherently a more complicated setup so I am not able to determine the cause from that, but for comparison have you looked at the UMA integration? This should give you a clear setup for creating a character at runtime.
 
I didn't realize the ResetDomain() function was public so I was able to clear the cache used by GetCachedParentComponent() and that seems to have resolved the issue. Thanks!
 
Back
Top