質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Q&A

解決済

2回答

3115閲覧

Unity で 「NullReferenceException: Object reference not set to an instance of an object」というメッセージが出る

sakiju

総合スコア2

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

0グッド

0クリップ

投稿2022/07/21 12:36

前提

スターターアセットのThirdPersonControllerでエラーが発生しました 昨日までエラーはなく正常に動きました
Unityで「NullReferenceException: Object reference not set to an instance of an object」というメッセージが出ます。

実現したいこと

エラーが出ないようにしたい

発生している問題・エラーメッセージ

NullReferenceException: Object reference not set to an instance of an object StarterAssets.ThirdPersonController.Move () (at Assets/StarterAssets/ThirdPersonController/Scripts/ThirdPersonController.cs:209) StarterAssets.ThirdPersonController.Update () (at Assets/StarterAssets/ThirdPersonController/Scripts/ThirdPersonController.cs:122)

該当のソースコード

unity

1 2 private void Start() 3 { 4 _hasAnimator = TryGetComponent(out _animator); 5 _controller = GetComponent<CharacterController>(); 6 _input = GetComponent<StarterAssetsInputs>(); 7 8 AssignAnimationIDs(); 9 10 // reset our timeouts on start 11 _jumpTimeoutDelta = JumpTimeout; 12 _fallTimeoutDelta = FallTimeout; 13 } 14 15 private void Update() 16 { 17 _hasAnimator = TryGetComponent(out _animator); 18 19 JumpAndGravity(); 20 GroundedCheck(); 21ここ Move(); 22 } 23 24 private void LateUpdate() 25 { 26 CameraRotation(); 27 } 28 29 private void AssignAnimationIDs() 30 { 31 _animIDSpeed = Animator.StringToHash("Speed"); 32 _animIDGrounded = Animator.StringToHash("Grounded"); 33 _animIDJump = Animator.StringToHash("Jump"); 34 _animIDFreeFall = Animator.StringToHash("FreeFall"); 35 _animIDMotionSpeed = Animator.StringToHash("MotionSpeed"); 36 } 37 38 private void GroundedCheck() 39 { 40 // set sphere position, with offset 41 Vector3 spherePosition = new Vector3(transform.position.x, transform.position.y - GroundedOffset, transform.position.z); 42 Grounded = Physics.CheckSphere(spherePosition, GroundedRadius, GroundLayers, QueryTriggerInteraction.Ignore); 43 44 // update animator if using character 45 if (_hasAnimator) 46 { 47 _animator.SetBool(_animIDGrounded, Grounded); 48 } 49 } 50 51 private void CameraRotation() 52 { 53 // if there is an input and camera position is not fixed 54 if (_input.look.sqrMagnitude >= _threshold && !LockCameraPosition) 55 { 56 _cinemachineTargetYaw += _input.look.x * Time.deltaTime; 57 _cinemachineTargetPitch += _input.look.y * Time.deltaTime; 58 } 59 60 // clamp our rotations so our values are limited 360 degrees 61 _cinemachineTargetYaw = ClampAngle(_cinemachineTargetYaw, float.MinValue, float.MaxValue); 62 _cinemachineTargetPitch = ClampAngle(_cinemachineTargetPitch, BottomClamp, TopClamp); 63 64 // Cinemachine will follow this target 65 CinemachineCameraTarget.transform.rotation = Quaternion.Euler(_cinemachineTargetPitch + CameraAngleOverride, _cinemachineTargetYaw, 0.0f); 66 } 67 68 private void Move() 69 { 70 // set target speed based on move speed, sprint speed and if sprint is pressed 71 float targetSpeed = _input.sprint ? SprintSpeed : MoveSpeed; 72 73 // a simplistic acceleration and deceleration designed to be easy to remove, replace, or iterate upon 74 75 // note: Vector2's == operator uses approximation so is not floating point error prone, and is cheaper than magnitude 76 // if there is no input, set the target speed to 0 77 if (_input.move == Vector2.zero) targetSpeed = 0.0f; 78 79 // a reference to the players current horizontal velocity 80 float currentHorizontalSpeed = new Vector3(_controller.velocity.x, 0.0f, _controller.velocity.z).magnitude; 81 82 float speedOffset = 0.1f; 83 float inputMagnitude = _input.analogMovement ? _input.move.magnitude : 1f; 84 85 // accelerate or decelerate to target speed 86 if (currentHorizontalSpeed < targetSpeed - speedOffset || currentHorizontalSpeed > targetSpeed + speedOffset) 87 { 88 // creates curved result rather than a linear one giving a more organic speed change 89 // note T in Lerp is clamped, so we don't need to clamp our speed 90 _speed = Mathf.Lerp(currentHorizontalSpeed, targetSpeed * inputMagnitude, Time.deltaTime * SpeedChangeRate); 91 92 // round speed to 3 decimal places 93 _speed = Mathf.Round(_speed * 1000f) / 1000f; 94 } 95 else 96 { 97 _speed = targetSpeed; 98 } 99 _animationBlend = Mathf.Lerp(_animationBlend, targetSpeed, Time.deltaTime * SpeedChangeRate); 100 101 // normalise input direction 102 Vector3 inputDirection = new Vector3(_input.move.x, 0.0f, _input.move.y).normalized; 103 104 // note: Vector2's != operator uses approximation so is not floating point error prone, and is cheaper than magnitude 105 // if there is a move input rotate player when the player is moving 106 if (_input.move != Vector2.zero) 107 { 108ここ _targetRotation = Mathf.Atan2(inputDirection.x, inputDirection.z) * Mathf.Rad2Deg + _mainCamera.transform.eulerAngles.y; 109 float rotation = Mathf.SmoothDampAngle(transform.eulerAngles.y, _targetRotation, ref _rotationVelocity, RotationSmoothTime); 110 111 // rotate to face input direction relative to camera position 112 transform.rotation = Quaternion.Euler(0.0f, rotation, 0.0f); 113 } 114 115 116 Vector3 targetDirection = Quaternion.Euler(0.0f, _targetRotation, 0.0f) * Vector3.forward; 117 118 // move the player 119 _controller.Move(targetDirection.normalized * (_speed * Time.deltaTime) + new Vector3(0.0f, _verticalVelocity, 0.0f) * Time.deltaTime); 120 121 // update animator if using character 122 if (_hasAnimator) 123 { 124 _animator.SetFloat(_animIDSpeed, _animationBlend); 125 _animator.SetFloat(_animIDMotionSpeed, inputMagnitude); 126 } 127 }

補足情報(FW/ツールのバージョンなど)

ここと書いているのが該当箇所です
unity 2020 3.25 f1 personal です

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

_mainCameraにオブジェクトが割り当てられていないみたいなので、Startメソッドに以下のコードを追加してみてください。

cs

1_mainCamera = Camera.main;

投稿2022/07/23 10:32

KomoriGameDev

総合スコア433

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

コード全体じゃないから何とも言えませんが、
_mainCameraメンバ変数が他の部分で使われていないし、startメソッドで初期化もされていないからこれが原因の可能性が極めて高そうですね。
正常に動いていたのはif (_input.move != Vector2.zero)がtrueになることがいままでなかったからでは?

投稿2022/07/21 20:59

RiaFeed

総合スコア2701

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sakiju

2022/07/22 03:33

直す方法として何が挙げられるのでしょうか
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問