I am roughly starting developer and I am at the moment engaged on a 3D recreation mission with third individual view.
Once I applied first ActionMap into my character controller, the whole lot labored fantastic, PlayerControls object has been known as inside the category with enter callbacks and it was sufficient for strolling, sprinting, leaping, and so forth. However once I created one other ActionMap for actions(assault, collect, work together) and determined to maneuver PlayerControls implementation into one other, centralized script, the whole lot 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 found out that Occasion is null in the beginning 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; 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 code pattern from the ActionMap enter script:
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);
}