Search Unity

ScreenToWorldPoint giving NullReferenceException

Discussion in 'Scripting' started by tfs_prime, Sep 25, 2018.

  1. tfs_prime

    tfs_prime

    Joined:
    Mar 11, 2018
    Posts:
    5
    I am testing a simple 2D game player controller script. I am using ScreenToWorldPoint to have the player follow the mouse location. The code looks ok and the values for x,y, and z are showing up ok. But when the script gets to invoking the ScreenToWorldPoint call for the Camera I get a NullReferenceException.

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. public class PlayerController : MonoBehaviour {
    4.     private Rigidbody ThisBody = null;
    5.     private Transform ThisTransform = null;
    6.     public bool MouseLook = true;
    7.     public string HorzAxis = "Horizontal";
    8.     public string VertAxis = "Vertical";
    9.     public string FireAxis = "Fire1";
    10.     public float MaxSpeed = 5f;
    11.     // Use this for initialization
    12.     void Awake () {
    13.         ThisBody = GetComponent<Rigidbody>();
    14.         ThisTransform = GetComponent<Transform>();
    15.     }
    16.     //
    17.     void FixedUpdate () {
    18.         // Get axis input values
    19.         float Horz = Input.GetAxis(HorzAxis);
    20.         float Vert = Input.GetAxis(VertAxis);
    21.         // set direction to move
    22.         Vector3 MoveDirection = new Vector3(Horz, 0.0f, Vert);
    23.      
    24.         //Clamp speed of player's ship
    25.         ThisBody.AddForce(MoveDirection.normalized * MaxSpeed);
    26.         ThisBody.velocity = new Vector3(
    27.             Mathf.Clamp(ThisBody.velocity.x, -MaxSpeed, MaxSpeed),
    28.             Mathf.Clamp(ThisBody.velocity.y, -MaxSpeed, MaxSpeed),
    29.             Mathf.Clamp(ThisBody.velocity.z, -MaxSpeed, MaxSpeed));
    30.         if (MouseLook)
    31.         {
    32.             Debug.Log("In Mouselook: position x is: " + Input.mousePosition.x + " position y is: " + Input.mousePosition.y + " position z is: " + +Input.mousePosition.z);
    33.             var x = Input.mousePosition.x;
    34.             var y = Input.mousePosition.y;
    35.             var z = Input.mousePosition.z;
    36.             Vector3 screenPosition = new Vector3(x,y,z);
    37.             Debug.Log("screenPosition is: " + screenPosition + " Location: " + Camera.main.ScreenToWorldPoint(position: screenPosition));
    38.             Vector3 MousePos = Camera.main.ScreenToWorldPoint(position: screenPosition);
    39.              MousePos = new Vector3(screenPosition.x, 0.0f, screenPosition.z);
    40.             Debug.Log("MousePosWorld: " + MousePos);
    41.             Vector3 LookDirection = MousePos - ThisTransform.position;
    42.             //rotation
    43.             ThisTransform.localRotation = Quaternion.LookRotation(LookDirection.normalized, Vector3.up);
    44.         }
    45.     }
    46. }
     
  2. samizzo

    samizzo

    Joined:
    Sep 7, 2011
    Posts:
    487
    So the NullReferenceException occurs on line 38 in the above code snippet? Do you have a main camera in your scene? To use `Camera.main` you should have a camera in the scene with the MainCamera tag set on it. Do you have such a camera in your scene?

    Sam
     
    DLGCreations likes this.
  3. tfs_prime

    tfs_prime

    Joined:
    Mar 11, 2018
    Posts:
    5
    :( Camera was not tagged, DUH!
     
    samizzo likes this.