I am roughly starting developer and I am at present engaged on a 3D sport venture with third individual view.
After I carried out first ActionMap into my character controller, every little thing labored tremendous, PlayerControls object has been known as inside the category with enter callbacks and it was sufficient for strolling, sprinting, leaping, and many others. However after I created one other ActionMap for actions(assault, collect, work together) and determined to maneuver PlayerControls implementation into one other, centralized script, every little thing crumbled down. Upon beginning play mode in unity I get a NullRefrenceException within the OnEnable cycle in PlayerLocomotionInput. After including some strains for debugging, I’ve discovered that Occasion is null firstly of LocomotionInput OnEnable cycle, as well as, I do not see a DontDestroyOnLoad folder within the hierarchy throughout play mode.
What I used to be making an attempt to do, is to make a category InputManager, create a singleton to entry objects of this class and create PlayerControls 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 code pattern from the ActionMap enter script:
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);
}