Code question - getting NullReferenceExceptions on build runs

volak

New member
I'm using the UMACharacterBuilder script to initialize my UMA players at runtime and I'm having NullReferenceException problems at runtime

CapsuleColliderPositioner.cs is throwing NullReferences on awake because its unable to find UltimateCharacterLocation. But ONLY on a build - not the editor.

This code in CharacterBuilder.cs adds the locomotion script - but only if in the editor?

Why?

I've changed it to add it all the time now - I just want to be sure I'm not breaking something


Code:
#if UNITY_EDITOR
            if (character.GetComponent<UltimateCharacterLocomotion>() == null) {
                var characterLocomotion = character.AddComponent<UltimateCharacterLocomotion>();
                if (!Application.isPlaying) {
                    // The Moving state should automatically be added.
                    var presetPath = UnityEditor.AssetDatabase.GUIDToAssetPath(c_MovingStateGUID);
                    if (!string.IsNullOrEmpty(presetPath)) {
                        var preset = UnityEditor.AssetDatabase.LoadAssetAtPath(presetPath, typeof(StateSystem.PersistablePreset)) as StateSystem.PersistablePreset;
                        if (preset != null) {
                            var states = characterLocomotion.States;
                            System.Array.Resize(ref states, states.Length + 1);
                            // Default must always be at the end.
                            states[states.Length - 1] = states[0];
                            states[states.Length - 2] = new StateSystem.State("Moving", preset, null);
                            characterLocomotion.States = states;
                        }
                    }
                }
            }
#endif
 
Last edited:
Good catch. That code block is within UNITY_EDITOR becuase UnityEditor.AssetDatabase.LoadAssetAtPath is only available within the Unity Editor so you can't use it at runtime but the locomotion should still be added.
 
Ok cool thanks!

I've made a few other fixes of minor inconvenience, but one I just found and is a bit of a big deal is how UltimateCharacterLocomotion is registered to KinematicCharacter in KinematicObjectManager.


Locomotion is registered during OnEnable - but KinematicCharacter depends on Locomotion object already having UltimateCharacterLocomotionHandler which is not added until last in character builder.

The only solution I've found is to set UltimateCharacterLocomotion enabled=false in Awake - then setting it enabled at the end of CharacterBuilder AddEssentials

But thats not ideal and theres probably a better way to coordinate this relationship that doesn't depend on UltimateCharacterLocomotion and UltimateCharacterLocomotionHandler both existing
 
Are you getting errors in the build? I created a Windows executable using my test scene that generates the character at runtime and it ran without any errors without making any changes.
 
Sorry you are right I should have steps to reproduce

So from the demo scene:

Import UMA
Import UMA Integration

Disable Nolan, add UMA_DCS and UMADynamicCharacterAvatar
Add UMACharacterBuilder to UMADynamicCharacterAvatar
(follow guide for UMA runtime integration basically)

Change Third Person Movement to "Combat"
Change Camera Controller to Combat view type as well

At this point you should be able to build it and get the NullReference CharacterLocomotion exception


But when running in editor - you'll notice the character will not turn with the camera as designed for Combat movement. This is because of my second issue with the KinematicObjectManager .
 
I wasn't getting the null reference error but I did notice the character not rotating like you mentioned. The fix was to move initializing the character with the KinematicObjectManager to Start and then only reinitialize with the KinematicObjectManager within OnEnabled if it has already been enabled. I made a build of this as well and it worked.
 
Top