Deactivate Character On Start Null Reference Exception

magique

Active member
Using Unity 2019.3.8f1 with the latest Third Person Controller. I followed the documentation with the following code:

C#:
/// <summary>
    /// Set the position and deactivate the character.
    /// </summary>
    private void Start()
    {
        var characterLocomotion = m_Character.GetComponent<UltimateCharacterLocomotion>();
        if (characterLocomotion != null) {
            characterLocomotion.SetPositionAndRotation(Vector3.zero, Quaternion.identity);
            characterLocomotion.SetActive(false);
        }
    }


However, when it runs, the SetActive function gets a null reference on the following code:

Code:
            m_GameObject.SetActive(active);

For m_GameObject.

The only thing I did different is that I'm not calling the SetPositionAndRotation portion because I just want to deactivate the character.
 
I tried adding the StePositionAndRotation part and it didn't get the null reference exception. Then I removed it again and it stopped giving the exception.

However, I'm not getting the results I really want anyway. What I really want is to just pause the player so he can't move or look around. This code completely deactivates it and even the listener is turned off. What is the best way to just pause the player from my own scripts?
 
I tried just doing the following:

C#:
 characterLocomotion.enabled = false;

And although it functionally does what I need, I get the following console error over and over:


IndexOutOfRangeException: Index was outside the bounds of the array.
Opsive.UltimateCharacterController.Game.KinematicObjectManager.SetCharacterMovementInputInternal (System.Int32 characterIndex, System.Single horizontalMovement, System.Single forwardMovement) (at Assets/Opsive/UltimateCharacterController/Scripts/Game/KinematicObjectManager.cs:546)
Opsive.UltimateCharacterController.Game.KinematicObjectManager.SetCharacterMovementInput (System.Int32 characterIndex, System.Single horizontalMovement, System.Single forwardMovement) (at Assets/Opsive/UltimateCharacterController/Scripts/Game/KinematicObjectManager.cs:535)
Opsive.UltimateCharacterController.Character.UltimateCharacterLocomotionHandler.Update () (at Assets/Opsive/UltimateCharacterController/Scripts/Character/UltimateCharacterLocomotionHandler.cs:71)
 
OK, I figured out a way. Not sure if it's the best way, but the following works:

Code:
        void EnablePlayer(bool value)
        {
            var characterLocomotion = Player.GetComponent<UltimateCharacterLocomotion>();
            var characterLocomotionHandler = Player.GetComponent<UltimateCharacterLocomotionHandler>();
            if (characterLocomotion != null && characterLocomotionHandler != null)
            {
                characterLocomotion.enabled = value;
                characterLocomotionHandler.enabled = value;
            }
        } // EnablePlayer()
 
Top