Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

NullReferenceException: Object reference not set to an instance of an object

Discussion in 'Scripting' started by Dogedoge, Aug 18, 2016.

  1. Dogedoge

    Dogedoge

    Joined:
    Aug 18, 2016
    Posts:
    7
    Hi,

    First time posting here. I have seen this error in multiple threads, but still haven't found a solution to my problem, and I didnt want to hijack other threads, so:

    Doing a top down game where you will walk with WASD and aim/look with mouse, was working but now I'm getting:

    NullReferenceException: Object reference not set to an instance of an object
    PlayerController.ControlMouse () (at Assets/Scripts/PlayerController.cs:35)
    PlayerController.Update () (at Assets/Scripts/PlayerController.cs:27)

    Im checking the referenced objects on 35 and 27, still cant find it, if anyone has a clue, Id be really grateful, thanks!

    Heres the script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(CharacterController))]
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     //Handling variables
    8.     public float rotationSpeed = 450; //360 så ett varv per sekund
    9.     public float walkSpeed = 5;
    10.     public float runSpeed = 8;
    11.  
    12.     //System variables
    13.     private Quaternion targetRotation;
    14.  
    15.     //Components
    16.     private CharacterController controller;
    17.     private Camera cam; //För mouse-styrningen
    18.  
    19.     void Start() {
    20.  
    21.         controller = GetComponent<CharacterController>();
    22.         cam = Camera.main;
    23.  
    24.     }
    25.  
    26.     void Update () {
    27.         ControlMouse();
    28.         //ControlWASD();
    29.     }
    30.  
    31.     void ControlMouse() {
    32.  
    33.         Vector3 mousePos = Input.mousePosition;
    34.         mousePos = cam.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, cam.transform.position.y - transform.position.y));
    35.         targetRotation = Quaternion.LookRotation(mousePos - new Vector3(transform.position.x,0,transform.position.z));
    36.         transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetRotation.eulerAngles.y, rotationSpeed * Time.deltaTime);
    37.  
    38.         Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")); //GetAxisRaw accelererar inte, snapp speed (?)
    39.         Vector3 motion = input;
    40.         motion *= (Mathf.Abs(input.x) == 1 && Mathf.Abs(input.z) == 1) ? .7f : 1;
    41.         motion *= (Input.GetButton("Run")) ? runSpeed : walkSpeed;
    42.         motion += Vector3.up * -8; //gravity
    43.  
    44.         controller.Move(motion * Time.deltaTime);
    45.     } }
    46.  
     
  2. kru

    kru

    Joined:
    Jan 19, 2013
    Posts:
    452
  3. Dogedoge

    Dogedoge

    Joined:
    Aug 18, 2016
    Posts:
    7
    Thanks mate!

    There was nothing wrong with the script, but for some reason my camera dropped its tag MainCamera to Untagged. No idea how that could've happened, but anyway, its fixed, thanks a lot! And thanks for the read as well! :)
     
  4. Samsagaz_El_Bravo

    Samsagaz_El_Bravo

    Joined:
    Nov 4, 2018
    Posts:
    1
    2 years later, I have suffered the same problem :( i lost 2 hours of my time...
     
  5. mruva

    mruva

    Joined:
    Apr 12, 2020
    Posts:
    4
    Hey Man you solved my issue! I was stucked with Cinemachine (camera set to perspective in 2D environment) getting the right mouseposition.... I checked the tag of my camera and it was set to untagged, I set it to MainCamera and it worked.... to whom is facing my same issue: if you aren't able to get right mouse position, check Main Camera tag and pass a Vector3 to mouse position with Z values the distance of your Cinemachine Camera. Hope it can help someone!