- Home /
Question by
YoussefSheta · Oct 21, 2021 at 07:00 PM ·
audiosourcemissingcomponentexception
There is no 'AudioSource' attached to the "Cube" game object, but a script is trying to access it.
I create a primitive cube and add an AudioSource to it and when I access it it works fine...
void Start()
{
this.gameObject.AddComponent<AudioSource>();
Au = this.GetComponent<AudioSource>();
}
but I have a condition that the audio plays only if the UI is not active .
if (!EG.UI.activeSelf)
{
Au = this.GetComponent<AudioSource>();
Au.volume = Mathf.Clamp01(collision.relativeVelocity.magnitude / 45);
Au.pitch = .25f;
Au.Play();
}
Then when the UI is active it gives me this error:
MissingComponentException: There is no 'AudioSource' attached to the "Cube" game object, but a script is trying to access it.
You probably need to add a AudioSource to the game object "Cube". Or your script needs to check if the component is attached before using it.
Piece.OnCollisionEnter (UnityEngine.Collision collision) (at Assets/Scripts/Piece.cs:119)
but in the scene the AudioSource is in the cube components...
Any idea why this happens?
cube.png
(52.3 kB)
Comment