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

[Error] "Object reference not set to an instance of an object"

Discussion in 'Scripting' started by bearejustin, Apr 20, 2021.

  1. bearejustin

    bearejustin

    Joined:
    Mar 6, 2021
    Posts:
    9
    Hi all I am not sure what this error is saying, as I am just trying to make an object follow my mouse.

    Code (CSharp):
    1.  
    2. public class DraggingScript : MonoBehaviour
    3. {
    4.    
    5.     private bool IsDragging;
    6.  
    7.     void Start()
    8.     {
    9.        
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.  
    16.         Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) - gameObject.transform.position;
    17.         transform.Translate(mousePosition);
    18.     }
    19. }
    20.  
    That is the entire code and the error occurs on - Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) - gameObject.transform.position;
     
  2. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    that script should work without any problems, make sure that you have saved the changes to the script in visual studio, i sometimes forget to click save and am than wondering why unity does stuff the script is not saying^^

    only thing missing in your script would be the
    using UnityEngine;
    in the beginning, but wouldnt you have this, your error would be a different one...

    so everything should be perfectly fine with what you have written
     
  3. sasuchi

    sasuchi

    Joined:
    Jun 18, 2019
    Posts:
    13
    What objects are in your scene?
    Do you have a camera in the scene?

    The script itself is fine, i just copied it and it works fine.
     
  4. bearejustin

    bearejustin

    Joined:
    Mar 6, 2021
    Posts:
    9
    The Object that the script is in is made inside of another script and then the script is added as a component after the creation of the gameobject, will this cause the problem?
     
  5. sasuchi

    sasuchi

    Joined:
    Jun 18, 2019
    Posts:
    13
    Are you creating the Object by using GameObject.Instantiate( ) with a reference to a prefab ?
    That should cause no problems :

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class DraggingScript : MonoBehaviour
    4. {
    5.     private bool IsDragging;
    6.  
    7.     void Update()
    8.     {
    9.         Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) - gameObject.transform.position;
    10.         transform.Translate(mousePosition);
    11.     }
    12. }
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class TestScript : MonoBehaviour
    4. {
    5.     [SerializeField]
    6.     GameObject followMouse;
    7.  
    8.     void Start()
    9.     {
    10.         GameObject go = Instantiate(followMouse);
    11.         go.AddComponent<DraggingScript>();
    12.     }
    13. }
    14.  
     
  6. TheNightglow

    TheNightglow

    Joined:
    Oct 1, 2018
    Posts:
    201
    depends on how its made, are you doing something like:
    GameObject newObj = Instantiate(oldObj);
    newObj.AddComponent<DraggingScript>();

    because that should work fine....

    but given that the only thing in the line
    Vector2 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition) - gameObject.transform.position;
    that could apply to the error
    "Object reference not set to an instance of an object"
    is the gameObject, it seems that something is really wrong with the way you created your object
     
  7. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    Camera.main looks for a camera in your scene tagged MainCamera. Make sure this is in your scene. It MUST have the MainCamera tag.
    Otherwise, the best thing to do is use Debug.Log and print out the values of anything that could be null on that line.
     
    Tzipy, bobisgod234 and bearejustin like this.
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    The answer is always the same... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.
     
    Brathnann likes this.
  9. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    I was waiting for this. :D
     
    Lurking-Ninja and Kurt-Dekker like this.
  10. bearejustin

    bearejustin

    Joined:
    Mar 6, 2021
    Posts:
    9
    My camera wasn't marked as main thanks for the help
     
    Kurt-Dekker likes this.