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

object reference not set to an instance of an object error

Discussion in 'Scripting' started by azurecodes, May 8, 2021.

  1. azurecodes

    azurecodes

    Joined:
    Apr 29, 2021
    Posts:
    33
    hi, so im trying to make a waypoint marker but however, it's givinthe object reference null error. how do i fix it? here is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class WaypointScript : MonoBehaviour
    7. {
    8.    
    9.     public Image img;
    10.    
    11.     public Transform target;
    12.     // UI Text to display the distance
    13.     public Text meter;
    14.     // To adjust the position of the icon
    15.     public Vector3 offset;
    16.  
    17.     private void Update()
    18.     {
    19.      
    20.         float minX = img.GetPixelAdjustedRect().width / 2;
    21.        
    22.         float maxX = Screen.width - minX;
    23.  
    24.        
    25.         float minY = img.GetPixelAdjustedRect().height / 2;
    26.        
    27.         float maxY = Screen.height - minY;
    28.  
    29.      
    30.         Vector2 pos = Camera.main.WorldToScreenPoint(target.position + offset);
    31.  
    32.         //check if behind
    33.         if (Vector3.Dot((target.position - transform.position), transform.forward) < 0)
    34.         {
    35.            
    36.             if (pos.x < Screen.width / 2)
    37.             {
    38.                
    39.                 pos.x = maxX;
    40.             }
    41.             else
    42.             {
    43.                
    44.                 pos.x = minX;
    45.             }
    46.         }
    47.  
    48.         pos.x = Mathf.Clamp(pos.x, minX, maxX);
    49.         pos.y = Mathf.Clamp(pos.y, minY, maxY);
    50.  
    51.        
    52.         img.transform.position = pos;
    53.        
    54.         meter.text = ((int)Vector3.Distance(target.position, transform.position)).ToString() + "m";
    55.     }
    56. }
     
  2. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    Maybe post error? it says you on what line in code is it
     
  3. azurecodes

    azurecodes

    Joined:
    Apr 29, 2021
    Posts:
    33
    Code (CSharp):
    1.  Vector2 pos = Camera.main.WorldToScreenPoint(target.position + offset);
    it says line 30
     
  4. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    So you "target" is null, did you set it in inspector?
     
  5. azurecodes

    azurecodes

    Joined:
    Apr 29, 2021
    Posts:
    33
    yes, if this is what you meant
    9C632DE0-1E84-4D59-A1E3-378017953B5C_4_5005_c.jpeg
     
  6. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,826
    Check your camera, you probably don't have any cameras with the tag "MainCamera".
     
  7. Kurt-Dekker

    Kurt-Dekker

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