Search Unity

Question Help me please

Discussion in 'C# Job System' started by Igor_423, Jan 27, 2023.

  1. Igor_423

    Igor_423

    Joined:
    Jan 27, 2023
    Posts:
    1
    Hello, I have a problem, I get the error NullReferenceException: Object reference not set to an instance of an object
    SG.PlayerLocomotion.Update () (at Assets/Player/Script/PlayerLocomotion.cs:39) and I'm new so I don't understand the error in the code

    here is the script where the error comes from


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. namespace SG{
    6.  
    7.  
    8.   public class PlayerLocomotion : MonoBehaviour
    9. {
    10.     Transform cameraObject;
    11.     InputHandler inputHandler;
    12.     Vector3 moveDirection;
    13.  
    14.     [HideInInspector]
    15.     public Transform myTransform;
    16.  
    17.     public new Rigidbody rigidbody;
    18.     public GameObject normalCamera;
    19.  
    20.     [Header("Stats")]
    21.     [SerializeField]
    22.     float movementSpeed = 5;
    23.     [SerializeField]
    24.     float rotationSpeed = 10;
    25.  
    26.     void Start()
    27.     {
    28.        rigidbody = GetComponent<Rigidbody>();
    29.        inputHandler = GetComponent<InputHandler>();
    30.        cameraObject = Camera.main.transform;
    31.        myTransform = transform;
    32.     }
    33.     public void Update()
    34.     {
    35.       float delta = Time.deltaTime;
    36.  
    37.       inputHandler.TickInput(delta);
    38.  
    39.       moveDirection = cameraObject.forward * inputHandler.vertical;
    40.       moveDirection += cameraObject.right * inputHandler.horizontal;
    41.       moveDirection.Normalize();
    42.  
    43.       float speed = movementSpeed;
    44.       moveDirection *= speed;
    45.  
    46.       Vector3 projectedVelocity = Vector3.ProjectOnPlane(moveDirection, normalVector);
    47.       rigidbody.velocity = projectedVelocity;
    48.  
    49.  
    50.     }
    51.    
    52.     #region Movement
    53.     Vector3 normalVector;
    54.     Vector3 targetPosition;
    55.  
    56.     private void HandleRotation(float delta)
    57.     {
    58.      Vector3 targetDir = Vector3.zero;
    59.      float moveOverride = inputHandler.moveAmount;
    60.  
    61.      targetDir.Normalize();
    62.      targetDir.y = 0;
    63.  
    64.      if (targetDir == Vector3.zero)
    65.          targetDir = myTransform.forward;
    66.      
    67.       float rs = rotationSpeed;
    68.  
    69.       Quaternion tr = Quaternion.LookRotation(targetDir);
    70.       Quaternion targetRotation = Quaternion.Slerp(myTransform.rotation, tr, rs * delta);
    71.  
    72.       myTransform.rotation = targetRotation;
    73.  
    74.     }
    75.      #endregion
    76. }
    77. }




    and here is the another script


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class InputHandler : MonoBehaviour
    6. {
    7.     public float horizontal;
    8.     public float vertical;
    9.     public float moveAmount;
    10.     public float mouseX;
    11.     public float mouseY;
    12.  
    13.     PlayerControls inputActions;
    14.  
    15.     Vector2 movementInput;
    16.     Vector2 cameraInput;
    17.  
    18.     public void OnEnable()
    19.     {
    20.      if (inputActions == null)
    21.      {
    22.         inputActions = new PlayerControls();
    23.         inputActions.PlayerMovement.Movement.performed += inputActions => movementInput = inputActions.ReadValue<Vector2>();
    24.         inputActions.PlayerMovement.Camera.performed += i => cameraInput = i.ReadValue<Vector2>();
    25.    
    26.      }
    27.  
    28.      inputActions.Enable();
    29.  
    30.  
    31.     }
    32.     private void OnDisable()
    33.     {
    34.         inputActions.Disable();
    35.     }
    36.    
    37.     public void TickInput(float delta)
    38.     {
    39.  
    40.     }
    41.    
    42.     private void MoveInput(float delta)
    43.     {
    44.      horizontal = movementInput.x;
    45.      vertical = movementInput.y;
    46.      moveAmount = Mathf.Clamp01(Mathf.Abs(horizontal) + Mathf.Abs(vertical));
    47.      mouseX = cameraInput.x;
    48.      mouseY = cameraInput.y;
    49.  
    50.     }
    51.  
    52. }
    53.  
    if someone can help me please
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Null reference means that something you referenced (i.e. tried to access, use etc.) is not there.

    If the line numbers in your code match the error (line 39), possibly either of the objects used on that line is null. You are doing things with cameraObject and inputHandler on that line. So start by checking those. And always also check if Inspector exposed fields/variables actually have something assigned. i.e. the fields with public prefix or [SerializeField] attribute.

    Use Debug.Log and try print out a if the variables there actually have an assigned value. You could do something like this before the line which causes the error:
    Code (CSharp):
    1. Debug.Log("is cameraObject null: " + (cameraObject == null));
    This will then print out "is cameraObject null: " and either false or true depending if the object is null or not.

    P.S. Your post shouldn't be in this section of the forum.
     
    Last edited: Jan 29, 2023
    Haxel0rd likes this.