Search Unity

Bug UnassignedReferenceException

Discussion in 'Scripting' started by Dio330go, Jul 6, 2023.

  1. Dio330go

    Dio330go

    Joined:
    Jun 2, 2023
    Posts:
    1
    I don't know what's happening and I am getting very mad because it's basic.
    I am getting this error:

    UnassignedReferenceException: The variable Text of CongratScript has not been assigned.
    You probably need to assign the Text variable of the CongratScript script in the inspector.
    CongratScript.Start () (at Assets/CongratScript.cs:28)


    I don't understand it because seems to me that the variable was assigned



    Btw I am using unity 2022.3.4f1. I not sure if it is a bug or I am just stupid
     

    Attached Files:

  2. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    453
    If I guess correctly, your script Text reference is to a prefab and not to your scene object. Reassign it by dragging your TextDisplay scene object to the field.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
  4. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    453
    It is not a NullReferenceException but an unassigned reference that appears to be assigned.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    UnassignedReferenceException is just a wrapper Unity adds to give you more information.

    This is explicitly stated in my link.

    Screen Shot 2023-07-06 at 6.51.46 AM.png

    It's still functionally just a nullref.

    And the answer is still the same.

    That just means you didn't find the one that was null. You're still on Step #1 - Identify what is null

    How to fix a NullReferenceException error

    https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

    Three steps to success:
    - Identify what is null <-- any other action taken before this step is WASTED TIME
    - Identify why it is null
    - Fix that
     
  6. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    453
    No, it is an unassigned reference, and if you take this information and look at his output, you'll see that the Text reference is unassigned yet in his editor screenshot it appears assigned. So we have clearly identified what is null, and trying to understand why it is null.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    No, you haven't identified what is null.

    What you have above might be non-null. GREAT!

    That just means you didn't find the one that is null. Did the script get inadvertently dragged onto some other GameObject where it isn't set up?

    Or was that property null at the time it was accessed, but now it is okay?

    ALL of these things are stuff I see every day, almost every hour. So just keep at it.
     
  8. KillDashNine

    KillDashNine

    Joined:
    Apr 19, 2020
    Posts:
    453
    THAT is null on CongratScript.cs:28.
    upload_2023-7-6_16-46-25.png
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,726
    Still unaddressed is this issue:

    There's only four GameObjects in the scene. Make sure another doesn't have this script as well. This happens ALL THE TIME... you drag a script into the scene and miss the target. You re-drag it. Congratulations. Now you have two CongratScripts.

    The computer is never wrong with these, regardless of how much you post about it or how "very mad" you get about it.
     
    Chubzdoomer likes this.