Search Unity

Resolved False Error UnassignedReferenceException

Discussion in 'Editor & General Support' started by MinhocaNice, Jun 1, 2020.

  1. MinhocaNice

    MinhocaNice

    Joined:
    May 3, 2020
    Posts:
    249
    I am getting this error:

    UnassignedReferenceException: The variable FleshSound of PlayerStats has not been assigned.
    You probably need to assign the FleshSound variable of the PlayerStats script in the inspector.
    UnityEngine.AudioSource.Play () (at C:/buildslave/unity/build/artifacts/generated/common/modules/Audio/AudioBindings.gen.cs:610)
    PlayerStats.Hurt (Single Damage) (at Assets/Scripts/PlayerStats.cs:82)
    PlayerMovement.Update () (at Assets/Scripts/Controllers/PlayerMovement.cs:79)


    Which tells the audio source FleshSound was not assigned in the inspector. And indeed it wasn't.
    However, it really annoys me that this happens because i specifically assigned this audio source in the Start () method in the script that gets this error:

    Code (CSharp):
    1.     void Start()
    2.     {
    3.         PlayerManagerScript = FindObjectOfType<PlayerManager>();
    4.         UI = FindObjectOfType<UIManager>();
    5.         FleshSound = GetComponent<AudioSource>(); //<-- Here//
    6.         PlayerMovementScript = GetComponent<PlayerMovement>();
    7.         HUD = GetComponent<PlayerHUD>();
    8.  
    What is the problem? The FleshSound is clearly assigned right there. The audio source is also inside the object it is calling GetComponent in. The error also happens much after Start, so it is not because what calls it sits in another Start or Awake and therefore calls it before the assignment.

    Did I find an actual bug here?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Would you mind showing a screenshot of the inspector of this object, with both the PlayerStats component and the AudioSource component on the same object?

    Have you tried putting Debug.Log() right after your GetComponent call to check that
    • It's actually running
    • FleshSound is not somehow null
    The other question is, as you said where is the error coming from? Is it a Start() method of a different object? Are you instantiating your PlayerStats object and then immediately trying to get the FleshSound from it? In either of these cases it's possible Start() hasn't run yet, and you might be better off putting your GetComponent() call inside Awake() instead, which runs earlier in the object's lifecycle than Start().

    Final question - are you certain there's not multiple instances of this class in the scene? Maybe the error is coming from a different instance than you think...
     
    MinhocaNice likes this.
  3. MinhocaNice

    MinhocaNice

    Joined:
    May 3, 2020
    Posts:
    249
    As I said, no, the error comes of a custom method called Hurt () in another script that is only called when the player receives damage, and that doesn't happen before or during Start () method. The PlayerStats object is not instantiated, it is already sitting in the scene.

    I was able to get around this problem by simply setting the Audio Source in the inspector, but this is not ideal. I would really like a more permanent solution than having to setting a variable manually in inspector.

    I don't know how to add images here, it seems to require a URL and obviously if I take a screenshot here it would be an offline image, which doesn't have URL.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    You can copy and paste images into the text box
     
  5. MinhocaNice

    MinhocaNice

    Joined:
    May 3, 2020
    Posts:
    249
    Here is the inspector, I couldn't put it whole because it didn't fit.
     

    Attached Files:

  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Is it possible it gets reassigned elsewhere in your script or another script?
     
  7. MinhocaNice

    MinhocaNice

    Joined:
    May 3, 2020
    Posts:
    249
  8. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    MinhocaNice and flashframe like this.