17.6 C
New York
Monday, April 28, 2025

Unit – Implementation Downside/Use of Singleton


I’m kind of preliminary developer and I’m at the moment engaged on a 3D recreation mission with a 3rd -person imaginative and prescient. After I carried out First ActionMap in my character’s controller, every little thing labored effectively, the PlayerControls object has been known as inside the class with enter name returns and it was sufficient to stroll, run, soar, and many others., however once I created one other motion map for actions (assault, assortment, work together) and determined to maneuver the implementation of playercontrols to a different, the centralized script is moody. When beginning the sport mode within the unit, I receive a nullrefrencexception within the uninable cycle within the playerlocomotioninput. After including some traces for purification, I found that the occasion is null at first of the inenible cycle of Locomotioninput, as well as, I don’t see a Dontdestroyonload folder within the hierarchy in the course of the recreation 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; personal set; }


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

    personal void OnEnable()
    {
        PlayerControls = new();
        PlayerControls.Allow();
    }

    personal void OnDisable()
    {
        PlayerControls.Disable();
    }
}

And a Script enter Script code pattern:

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

    personal void OnEnable()
    {
        InputManagerScript.Occasion.PlayerControls.PlayerLocomotionMap.Allow();
        InputManagerScript.Occasion.PlayerControls.PlayerLocomotionMap.SetCallbacks(this);
    }

    personal 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