【Unity】NullReferenceException: Object reference not set to an instance of an object の解決方を教えてください。
Error
1NullReferenceException: Object reference not set to an instance of an object 2TarodevController.PlayerController.OnEnable () (at Assets/Scripts/Player/PlayerController.cs:52)
が出て実行できません。
エラーが出るコード
C#
1using System.Collections.Generic; 2using System.Linq; 3using UnityEngine; 4using UnityEngine.InputSystem; 5 6namespace TarodevController { 7 public class PlayerController : MonoBehaviour, IPlayerController { 8 // Public for external hooks 9 public Vector3 Velocity { get; private set; } 10 public FrameInput Input { get; private set; } 11 public bool JumpingThisFrame { get; private set; } 12 public bool LandingThisFrame { get; private set; } 13 public Vector3 RawMovement { get; private set; } 14 public bool Grounded => _colDown; 15 16 private Vector3 _lastPosition; 17 private float _currentHorizontalSpeed, _currentVerticalSpeed; 18 19 private PlayerInput input; 20 private bool jump; 21 private Vector2 movement; 22 23 // This is horrible, but for some reason colliders are not fully established when update starts... 24 private bool _active; 25 void Awake() => Invoke(nameof(Activate), 0.5f); 26 void Activate() => _active = true; 27 28 void Start() 29 { 30 input = GetComponent<PlayerInput>(); 31 } 32 33 private void Update() { 34 if(!_active) return; 35 // Calculate velocity 36 Velocity = (transform.position - _lastPosition) / Time.deltaTime; 37 _lastPosition = transform.position; 38 39 GatherInput(); 40 RunCollisionChecks(); 41 42 CalculateWalk(); // Horizontal movement 43 CalculateJumpApex(); // Affects fall speed, so calculate before gravity 44 CalculateGravity(); // Vertical movement 45 CalculateJump(); // Possibly overrides vertical 46 47 MoveCharacter(); // Actually perform the axis movement 48 } 49///////////////////////////////////////////////////////////////////////////////////////////////////////// 50 void OnEnable() 51 { 52 input.actions["Move"].performed += OnMove; 53 input.actions["Move"].canceled += OnMove; 54 55 input.actions["Jump"].performed += OnJump; 56 input.actions["Jump"].canceled += OnJump; 57 } 58///////////////////////////////////////////////////////////////////////////////////////////////////////// 59 void OnDisable() 60 { 61 input.actions["Move"].performed -= OnMoveStop; 62 input.actions["Move"].canceled -= OnMoveStop; 63 64 input.actions["Jump"].performed -= OnJump; 65 input.actions["Jump"].canceled -= OnJump; 66 } 67 68 private void OnJump(InputAction.CallbackContext obj) 69 { 70 switch(obj.phase) 71 { 72 case InputActionPhase.Performed: 73 jump = true; 74 break; 75 case InputActionPhase.Canceled: 76 jump = false; 77 break; 78 } 79 } 80 81 private void OnMove(InputAction.CallbackContext obj) 82 { 83 movement = obj.ReadValue<Vector2>(); 84 } 85 86 private void OnMoveStop(InputAction.CallbackContext obj) 87 { 88 movement = Vector2.zero; 89 } 90 91 #region Gather Input 92 93 private void GatherInput() { 94 Input = new FrameInput { 95 JumpDown = jump, JumpUp = jump, X = movement.x 96 }; 97 if (Input.JumpDown) { 98 _lastJumpPressed = Time.time; 99 } 100 } 101 102 #endregion 103 104 #region Collisions 105 106 [Header("COLLISION")] [SerializeField] private Bounds _characterBounds; 107 [SerializeField] private LayerMask _groundLayer; 108 [SerializeField] private int _detectorCount = 3; 109 [SerializeField] private float _detectionRayLength = 0.1f; 110 [SerializeField] [Range(0.1f, 0.3f)] private float _rayBuffer = 0.1f; // Prevents side detectors hitting the ground 111 112 private RayRange _raysUp, _raysRight, _raysDown, _raysLeft; 113 private bool _colUp, _colRight, _colDown, _colLeft; 114 115 private float _timeLeftGrounded; 116 117 // We use these raycast checks for pre-collision information 118 private void RunCollisionChecks() { 119 // Generate ray ranges. 120 CalculateRayRanged(); 121 122 // Ground 123 LandingThisFrame = false; 124 var groundedCheck = RunDetection(_raysDown); 125 if (_colDown && !groundedCheck) _timeLeftGrounded = Time.time; // Only trigger when first leaving 126 else if (!_colDown && groundedCheck) { 127 _coyoteUsable = true; // Only trigger when first touching 128 LandingThisFrame = true; 129 } 130 131 _colDown = groundedCheck; 132 133 // The rest 134~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 135 省略 136~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 137 138 positionToMoveTo = posToTry; 139 } 140 } 141 142 #endregion 143 } 144}
出来れば早めにお願いします。┏(_ _)┓

バッドをするには、ログインかつ
こちらの条件を満たす必要があります。