Search Unity

Hi im new with unity and i need help with this error . its for a 2d topdown

Discussion in 'Scripting' started by Aqua_Peaches, Apr 10, 2021.

  1. Aqua_Peaches

    Aqua_Peaches

    Joined:
    Apr 10, 2021
    Posts:
    2
    The name Of the Script is lookMouse.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class faceMouse : MonoBehaviour
    6. {
    7.  
    8.  
    9.  
    10.     void Awake()
    11.     {
    12.         mainCamera = Camera.main;
    13.     }
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.         lookMouse();
    18.     }
    19.  
    20.     public void lookMouse()
    21.     {
    22.         Vector3 mousePosition = Input.mousePosition;
    23.        
    24.         mousePosition =Camera.main.ScreenToWorldPoint(mousePosition);
    25.  
    26.         Vector2 mouseDirection = new Vector2(
    27.             mousePosition.x - transform.position.x,
    28.             mousePosition.y - transform.position.y);
    29.  
    30.         transform.up = mouseDirection;
    31.     }
    32.  
    33. }
    34.  
     
  2. Aqua_Peaches

    Aqua_Peaches

    Joined:
    Apr 10, 2021
    Posts:
    2
    This is the error
    NullReferenceException: Object reference not set to an instance of an object
    faceMouse.lookMouse () (at Assets/Script/Player/faceMouse.cs:24)
    faceMouse.Update () (at Assets/Script/Player/faceMouse.cs:17)

    i hope i can understand why this is happening
    but right now i have no ideea

    O also this script is from a youtube tutorial How to face The Mouse Position in 2D
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Null errors means something doesn't have a value. My guess is it's Camera.main is returning null. You need to make sure you have a camera in your scene tagged MainCamera.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    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.