Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Looking For Contributor Script going Haywire

Discussion in 'Non Commercial Collaboration' started by Paloo, Mar 21, 2021.

  1. Paloo

    Paloo

    Joined:
    Sep 23, 2020
    Posts:
    10
    I'm trying to make player rotation from this tutorial. But it says
    NullReferenceException: Object reference not set to an instance of an object
    Movement.Update () (at Assets/Movement.cs:63).
    Here is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Movement : MonoBehaviour
    6. {
    7.  
    8.     public float movementSpeed;
    9.     public float rotationSpeed;
    10.     public float rotX;
    11.     public float rotY;
    12.     public float rotZ;
    13.  
    14.     // Use this for initialization
    15.     void Start()
    16.     {
    17.  
    18.     }
    19.  
    20.     //Update is called once per frame
    21.     void FixedUpdate()
    22.     {
    23.  
    24.         if (Input.GetKey(KeyCode.LeftShift) && Input.GetKey("w"))
    25.         {
    26.             transform.position += transform.TransformDirection(Vector3.forward) * Time.deltaTime * movementSpeed * 2.5f;
    27.         }
    28.         else if (Input.GetKey("w") && !Input.GetKey(KeyCode.LeftShift))
    29.         {
    30.             transform.position += transform.TransformDirection(Vector3.forward) * Time.deltaTime * movementSpeed;
    31.         }
    32.         else if (Input.GetKey("s"))
    33.         {
    34.             transform.position -= transform.TransformDirection(Vector3.forward) * Time.deltaTime * movementSpeed;
    35.         }
    36.  
    37.         if (Input.GetKey("a") && !Input.GetKey("d"))
    38.         {
    39.             transform.position += transform.TransformDirection(Vector3.left) * Time.deltaTime * movementSpeed;
    40.         }
    41.         else if (Input.GetKey("d") && !Input.GetKey("a"))
    42.         {
    43.             transform.position -= transform.TransformDirection(Vector3.left) * Time.deltaTime * movementSpeed;
    44.         }
    45.     }
    46.  
    47.     void Update()
    48.     {
    49.  
    50.         rotX -= Input.GetAxis("Mouse Y") * Time.deltaTime * rotationSpeed;
    51.         rotY += Input.GetAxis("Mouse X") * Time.deltaTime * rotationSpeed;
    52.  
    53.         if (rotX < -90)
    54.         {
    55.             rotX = -90;
    56.         }
    57.         else if (rotX > 90)
    58.         {
    59.             rotX = 90;
    60.         }
    61.  
    62.         transform.rotation = Quaternion.Euler(0, rotY, 0);
    63.         GameObject.FindWithTag("MainCamera").transform.rotation = Quaternion.Euler(rotX, rotY, 0);
    64.     }
    65. }
    If you know what to do please help.