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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

NullReferenceException on GameObject.name but the name still prints in Debug.Log

Discussion in 'Scripting' started by snoutie, Mar 23, 2021.

  1. snoutie

    snoutie

    Joined:
    Jan 15, 2017
    Posts:
    2
    Hi everybody,
    I'm getting a NullReferenceException on a GameObject but the name of the GameObject still prints.

    upload_2021-3-23_20-23-53.png
    upload_2021-3-23_20-24-2.png

    upload_2021-3-23_20-24-13.png
    upload_2021-3-23_20-24-21.png

    Code (CSharp):
    1. public class EquipmentManager : MonoBehaviour
    2. {
    3.     public GameObject humanoidPrefab;
    4.  
    5.     void Start()
    6.     {
    7.         Debug.Log(humanoidPrefab.name);
    8.     }
    9. }

    Is this a bug or am I missing something?
     

    Attached Files:

  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,742
    You probably have two copies of EquipmentManager in your scene. The one you're paying attention to is correctly set up, and then another one you didn't know existed where humanoidPrefab is null.
     
    snoutie likes this.
  3. snoutie

    snoutie

    Joined:
    Jan 15, 2017
    Posts:
    2
    Yes, you are right thanks a lot
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    This approach is smart, and I see what you were thinking. Another tool to add to your kit is to print just the
    name
    , which would give you the name of the GameObject where this script is. That would reveal two of them firing and one of them would be a name that you don't expect it to be.

    I've done this exact thing before too. It's easy when you miss the drag by a pixel or two, or the mouse jiggles as you let go and you don't notice.
     
    snoutie likes this.