I am a newbie developer and I am at the moment engaged on a 3D sport undertaking with a 3rd particular person view.
After I applied my first ActionMap
into my character controller, every thing labored wonderful. The PlayerControls
object was saved inside the category with enter callbacks and it was sufficient for strolling, sprinting, leaping, and so on.
However once I created one other ActionMap
for actions (assault, collect, work together) and determined to maneuver PlayerControls
implementation into one other, centralized script, every thing crumbled down.
Upon beginning play mode in Unity, I get a NullRefrenceException
within the OnEnable
technique in PlayerLocomotionInput
. After including some traces for debugging, I’ve found out that Occasion
is null when LocomotionInput.OnEnable()
known as. As well as, I do not see a DontDestroyOnLoad
folder within the hierarchy throughout play mode.
What I used to be attempting 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);
}