Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

NullReferenceException Error

Discussion in 'Scripting' started by AviBomjan, Nov 10, 2018.

  1. AviBomjan

    AviBomjan

    Joined:
    Oct 27, 2018
    Posts:
    5
    This is the error message i get when i run it.

    NullReferenceException: Object reference not set to an instance of an object Player.Update () (at Assests/Scripts/Player.cs: 16)

    This is the code.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Player : MonoBehaviour
    {
    //Variables
    public float movementSpeed;

    //Functions
    void Update()
    {
    //Player movement
    Plane playerPlane = new Plane(Vector3.up, transform.position);
    Ray ray = UnityEngine.Camera.main.ScreenPointToRay(Input.mousePosition);
    float hitDistance = 0.0f;

    if (playerPlane.Raycast(ray, out hitDistance))
    {
    Vector3 targetPoint = ray.GetPoint(hitDistance);
    Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
    if (Input.GetMouseButtonDown(0))
    {
    print(targetPoint);
    }
    }
    }
    }
     
  2. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    137
    The error tells you that line 16 contains the problematic call. If there are no other lines in your code than the ones you showed, there is a high probability that there is no main camera in your scene. In editor, simply double-click on the error message, and usually it will take you to the offending line in the code.

    -ch
     
  3. AviBomjan

    AviBomjan

    Joined:
    Oct 27, 2018
    Posts:
    5
    That's all the code. And i have a main camera. I don't know what's the problem. :(
     
  4. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    137
    Double-click on the offending line, and copy the line that is throwing the exception here, together with the error you are getting. Maybe we can figure it out then.
     
  5. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    2,002
    Urgent
    Use code tags properly.

    upload_2018-11-10_13-17-49.png
     
  6. AviBomjan

    AviBomjan

    Joined:
    Oct 27, 2018
    Posts:
    5
    Ray ray = UnityEngine.Camera.main.ScreenPointToRay(Input.mousePosition);
    This is the code that's highlighted.
     
  7. AviBomjan

    AviBomjan

    Joined:
    Oct 27, 2018
    Posts:
    5
    Sorry about that. New here, going to rename this post.
     
  8. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    137
    In that case, einter Camera or Input aren't defined. Break up the code to narrow down the issue:
    Code (CSharp):
    1. Camera mainCam = UnityEngine.Camera.main;
    2. if (mainCam == null) Debug.Log("No Camera!");
    3. Vector3  mp = Input.mousePosition;
    4. Ray r = maincam.ScreenPoint(mp);
     
  9. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    2,002
    No problem, looks better. If you just edit post and apply code tags, so look similar like @csofranz , it will be spot on.

    upload_2018-11-10_13-36-44.png

    PS. Just let you know, you got post duplicate btw.