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:
```csharp
void Start()
{
PlayerManagerScript = FindObjectOfType<PlayerManager>();
UI = FindObjectOfType<UIManager>();
FleshSound = GetComponent<AudioSource>(); //<-- Here//
PlayerMovementScript = GetComponent<PlayerMovement>();
HUD = GetComponent<PlayerHUD>();
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.
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…
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.
Searching for other people having this error, it seems it’s usually because the error is coming from another instance of that component on either the same object or a different game object, and you didn’t realize you had this component on another one. Try something like the answer here: