【Unity】NullReferenceException: Object reference not set to an instance of an object の解決方を教えてください。
Error
NullReferenceException: Object reference not set to an instance of an object TarodevController.PlayerController.OnEnable () (at Assets/Scripts/Player/PlayerController.cs:52)
が出て実行できません。
エラーが出るコード
C#
using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.InputSystem; namespace TarodevController { public class PlayerController : MonoBehaviour, IPlayerController { // Public for external hooks public Vector3 Velocity { get; private set; } public FrameInput Input { get; private set; } public bool JumpingThisFrame { get; private set; } public bool LandingThisFrame { get; private set; } public Vector3 RawMovement { get; private set; } public bool Grounded => _colDown; private Vector3 _lastPosition; private float _currentHorizontalSpeed, _currentVerticalSpeed; private PlayerInput input; private bool jump; private Vector2 movement; // This is horrible, but for some reason colliders are not fully established when update starts... private bool _active; void Awake() => Invoke(nameof(Activate), 0.5f); void Activate() => _active = true; void Start() { input = GetComponent<PlayerInput>(); } private void Update() { if(!_active) return; // Calculate velocity Velocity = (transform.position - _lastPosition) / Time.deltaTime; _lastPosition = transform.position; GatherInput(); RunCollisionChecks(); CalculateWalk(); // Horizontal movement CalculateJumpApex(); // Affects fall speed, so calculate before gravity CalculateGravity(); // Vertical movement CalculateJump(); // Possibly overrides vertical MoveCharacter(); // Actually perform the axis movement } ///////////////////////////////////////////////////////////////////////////////////////////////////////// void OnEnable() { input.actions["Move"].performed += OnMove; input.actions["Move"].canceled += OnMove; input.actions["Jump"].performed += OnJump; input.actions["Jump"].canceled += OnJump; } ///////////////////////////////////////////////////////////////////////////////////////////////////////// void OnDisable() { input.actions["Move"].performed -= OnMoveStop; input.actions["Move"].canceled -= OnMoveStop; input.actions["Jump"].performed -= OnJump; input.actions["Jump"].canceled -= OnJump; } private void OnJump(InputAction.CallbackContext obj) { switch(obj.phase) { case InputActionPhase.Performed: jump = true; break; case InputActionPhase.Canceled: jump = false; break; } } private void OnMove(InputAction.CallbackContext obj) { movement = obj.ReadValue<Vector2>(); } private void OnMoveStop(InputAction.CallbackContext obj) { movement = Vector2.zero; } #region Gather Input private void GatherInput() { Input = new FrameInput { JumpDown = jump, JumpUp = jump, X = movement.x }; if (Input.JumpDown) { _lastJumpPressed = Time.time; } } #endregion #region Collisions [Header("COLLISION")] [SerializeField] private Bounds _characterBounds; [SerializeField] private LayerMask _groundLayer; [SerializeField] private int _detectorCount = 3; [SerializeField] private float _detectionRayLength = 0.1f; [SerializeField] [Range(0.1f, 0.3f)] private float _rayBuffer = 0.1f; // Prevents side detectors hitting the ground private RayRange _raysUp, _raysRight, _raysDown, _raysLeft; private bool _colUp, _colRight, _colDown, _colLeft; private float _timeLeftGrounded; // We use these raycast checks for pre-collision information private void RunCollisionChecks() { // Generate ray ranges. CalculateRayRanged(); // Ground LandingThisFrame = false; var groundedCheck = RunDetection(_raysDown); if (_colDown && !groundedCheck) _timeLeftGrounded = Time.time; // Only trigger when first leaving else if (!_colDown && groundedCheck) { _coyoteUsable = true; // Only trigger when first touching LandingThisFrame = true; } _colDown = groundedCheck; // The rest ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 省略 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ positionToMoveTo = posToTry; } } #endregion } }
出来れば早めにお願いします。┏(_ _)┓
まだ回答がついていません
会員登録して回答してみよう