Search Unity

Follow cursor script crash

Discussion in 'Scripting' started by Colatran, Jul 19, 2019.

  1. Colatran

    Colatran

    Joined:
    Jul 7, 2019
    Posts:
    14
    Hi
    I was looking for a script to make an object follow the cursor
    And very time I look for a script to make a object follow the cursor, I always find something like this:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MouseMove2D : MonoBehaviour
    6. {
    7.     private Vector3 mousePosition;
    8.     public float moveSpeed = 0.1f;
    9.  
    10.     void Update()
    11.     {
    12.         if (Input.GetButtonDown("Fire1"))
    13.         {
    14.             mousePosition = Input.mousePosition;
    15.             mousePosition = Camera.main.ScreenToWorldPoint(mousePosition);
    16.             transform.position = Vector2.Lerp(transform.position, mousePosition, moveSpeed);
    17.         }
    18.  
    19.     }
    20. }
    But whenever I use the script, the game crashes and it gives me this error:

    NullReferenceException: Object reference not set to an instance of an object
    MouseMove2D.Update () (at Assets/Scripts/MouseMove2D.cs:15)​

    Any help is appreciated :)
     
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Does a main camera exist in the scene?
    Camera.main
    is a shorthand for
    GameObject.FindWithTag("Main Camera");
    .
    Ensure you have a Camera attached to a GameObject with the tag "Main Camera" in your scene.

    Also, because
    Camera.main
    is a
    FindWithTag()
    call, you don't want to be calling it every frame. Cache the reference instead:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class MouseMove2D : MonoBehaviour
    5. {
    6.     private Vector3 mousePosition;
    7.     public float moveSpeed = 0.1f;
    8.  
    9.     private Camera mainCamera;
    10.  
    11.     void Start()
    12.     {
    13.         mainCamera = Camera.main;
    14.     }
    15.  
    16.     void Update()
    17.     {
    18.         if (Input.GetButtonDown("Fire1"))
    19.         {
    20.             mousePosition = Input.mousePosition;
    21.             mousePosition = mainCamera.ScreenToWorldPoint(mousePosition);
    22.             transform.position = Vector2.Lerp(transform.position, mousePosition, moveSpeed);
    23.         }
    24.     }
    25. }
     
  3. Colatran

    Colatran

    Joined:
    Jul 7, 2019
    Posts:
    14
    Ok.
    Thanks for your tip.
    Managed to solve the issue.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MouseMove2D : MonoBehaviour
    6. {
    7.     private Vector3 mousePosition;
    8.     public float moveSpeed = 0.1f;
    9.  
    10.     public Camera mainCamera;
    11.  
    12.     void Update()
    13.     {
    14.         mousePosition = Input.mousePosition;
    15.         mousePosition = mainCamera.ScreenToWorldPoint(mousePosition);
    16.         transform.position = Vector2.Lerp(transform.position, mousePosition, moveSpeed);
    17.     }
    18. }
    Thanks again!!