Hi, when I enter the play mode I get this error constantly:
ArgumentNullException: Value cannot be null.
Parameter name: source
UnityEngine.AudioSource.set_pitch (System.Single value) (at <8863343ba0a7456391bfab2a31783ce3>:0)
PauseMenu.Update () (at Assets/Scripts/UI/PauseMenu.cs:45)
The code that it is referring is this one:
foreach (AudioSource audioS in allTheAudios)
{
if (Time.timeScale < 1f)
{
audioS.pitch = Time.timeScale + 0.0001f;
}
else
{
audioS.pitch = 1;
}
}
Why is this happening?
Thanks.
What is allTheAudios? How is it being assigned?
Is it possible that it contains null/destroyed audio sources?
The allAudios is an AudioSource [ ] that finds all AudioSources in the scene.
Also, I don’t think that destroyed AudioSources are the problem, because the error appears right when entering plat mode.
What line is this on? There is no line 45 above. Is it the reference to the AudioSource?
Whatever it is, same process applies, no exceptions:
How to fix a NullReferenceException error
https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/
Steps to success:
- Identify what is null
- Identify why it is null
- Fix that
Also, wouldn’t your logic be more-accurately replaced with simply:
audioS.pitch = Mathf.Clamp( Time.timeScale, 0.0001f, 1.0f);
… assuming pitch of 0.0001 isn’t “too slow” for the audio system… not sure if it gracefully handles pitch going to zero.