- Home /
Weird audio problem
What the hell?? So first off I want to say that there is probably nothing wrong with the code. Today I was working on my project. I was trying to add sound effects to my other objects.. And I did. And then suddenly 2 errors popped up. On both of these errors I was basically trying to play Audio through script.
'AudioSource' does not contain a definition for 'clip' and no extension method 'clip' accepting a first argument of type 'AudioSource' could be found (are you missing a using directive or an assembly reference?
'AudioSource' does not contain a definition for 'Play' and no extension method 'Play' accepting a first argument of type 'AudioSource' could be found (are you missing a using directive or an assembly reference?)
Here is my code but again, there is 100% nothing wrong with it since it worked fine before. Also its not completely the same as I changed it a bit after trying to solve the problem. But either way the script is probably alright.
public void ChangeLevel()
{
StartCoroutine(delayedLoad());
}
private IEnumerator delayedLoad()
{
AudioSource audio = GetComponent<AudioSource>();
audio.clip = OtherClip;
audio.Play();
Player.SetActive(false);
FadeOutPanel.SetActive(true);
FadeOutAnim.enabled = true;
yield return new WaitForSecondsRealtime(0.45f);
SceneManager.LoadScene(0);
}
So does anyone have any idea how could these errors just pop up after adding couple AudioSource components to gameobjects?
Thanks.
UPDATE!!
If I try...
AudioSource audio = GetComponent<AudioSource>();
if(audio == null)
{
print("AUDIO NOT RECOGNIZED");
}
else if(audio != null)
{
print("AUDIO RECOGNIZED");
}
it prints message AUDIO NOT RECOGNIZED, that means that even though Audio source component is attached it doesnt recognize it.
Does your script have
using UnityEngine.Audio;
at the top? That would do it.
Doesnt make a difference. Again, it worked before and I didnt modify the script at all.
Answer by tanoshimi · Nov 22, 2016 at 12:29 PM
Did you by any chance rename your script as AudioSource.cs?
Woah, just as I fixed the problem you answered it, exacly. When I was adding Audio Source gameobjects I ins$$anonymous$$d added a script named Audio Source by a mistake. Just realized that thanks either way!
Your answer
Follow this Question
Related Questions
master background music toggle not working after changing scenes 1 Answer
Audiosource.Play() not working 0 Answers
audio.Play(); not working while audio.Stop(); works fine when the audio is played on awake 1 Answer
AudioSource.clip.time won't work? 2 Answers
Assign a clip to an AudioSource when it finishes playing. 1 Answer