23.9 C
New York
Monday, April 28, 2025

UNITY – Exception of null reference when accessing ActionMap saved in a single


I’m a developer for newbies and I’m presently engaged on a 3D sport mission with a 3rd -person imaginative and prescient.

Once I carried out my first ActionMap In my character’s controller, the whole lot labored effectively. He PlayerControls The article was saved inside the category with enter name returns and it was sufficient to stroll, run, leap, and so forth.

However once I created one other ActionMap For actions (assault, collect, work together) and determined to maneuver PlayerControls Implementation in one other centralized script, the whole lot collapsed.

When beginning the sport mode within the unit, I get a NullRefrenceException in it OnEnable Technique in PlayerLocomotionInput. After including some strains for purification, I found that Occasion It’s void when LocomotionInput.OnEnable() is named. Moreover, I do not see a DontDestroyOnLoad folder within the hierarchy throughout replica mode. What I used to be attempting to do is make a category enter mana, create a single to entry objects of this class and create the playcontrol object that may be accessed elsewhere:

public class InputManagerScript : MonoBehaviour
{
    public static InputManagerScript Occasion;

    public PlayerControls PlayerControls { get; non-public set; }


    non-public void Awake()
    {
        if (Occasion != null && Occasion != this)
        {
            Destroy(this.gameObject);
            return;
        }
        else
        {
            Occasion = this;
            DontDestroyOnLoad(gameObject);
        }
    }

    non-public void OnEnable()
    {
        PlayerControls = new();
        PlayerControls.Allow();
    }

    non-public void OnDisable()
    {
        PlayerControls.Disable();
    }
}

And a Script enter Script code pattern:

public class PlayerLocomotionInput : MonoBehaviour, PlayerControls.IPlayerLocomotionMapActions
{
    #area Variables and fields
    #endregion

    non-public void OnEnable()
    {
        InputManagerScript.Occasion.PlayerControls.PlayerLocomotionMap.Allow();
        InputManagerScript.Occasion.PlayerControls.PlayerLocomotionMap.SetCallbacks(this);
    }

    non-public void OnDisable()
    {
        InputManagerScript.Occasion.PlayerControls.PlayerLocomotionMap.Disable();
        InputManagerScript.Occasion.PlayerControls.PlayerLocomotionMap.RemoveCallbacks(this);
    } 

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles