Search Unity

Question for some reason unity is giving me this error

Discussion in 'Scripting' started by Lenticularic, Jun 20, 2021.

  1. Lenticularic

    Lenticularic

    Joined:
    Nov 16, 2020
    Posts:
    46
    I am confusion, I've used these exact lines before and it didn't give the error.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Movement : MonoBehaviour
    6. {
    7.  
    8.  
    9.     void Update()
    10.     {
    11.    
    12.    
    13.     GameObject Index = GameObject.Find("candyindex"); //finding the ui gameObject
    14.     candyManagement Management = Index.GetComponent<candyManagement>(); //getting the script from that ui object
    15.  
    16.        
    17.     }
    18.  
    19. }
    20.  
    the error is " NullReferenceException: Object reference not set to an instance of an object "
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    There is no active GameObject named "candyindex" in the scene, so your Index variable is being given the value
    null
    on line 13.

    I also highly recommend against doing GameObject.Find() in general but especially in Update. Just do it once in Start() and cache the result in a variable.
     
    Vryken, Lenticularic and Bunny83 like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,735
    It is not necessary to post for null reference. It's MUCH faster to just find it and fix it with three easy steps.

    Why? Because 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.

    Here is a clean analogy of the actual underlying problem of a null reference exception:

    https://forum.unity.com/threads/nul...n-instance-of-an-object.1108865/#post-7137032
     
    Lenticularic likes this.
  4. Lenticularic

    Lenticularic

    Joined:
    Nov 16, 2020
    Posts:
    46
    thanks, I'll keep your tip in mind! (it worked)
     
  5. Lenticularic

    Lenticularic

    Joined:
    Nov 16, 2020
    Posts:
    46
    thank you too, I'll keep this in future projects AND this one, this will help a lot!