- Home /
@script RequireComponent(AudioSource) does not work
Hi,
I´m going through the Unity Game Dev Essentials right now and having a little problem with the
@script RequireComponent(AudioSource)
command.
It simply doesn´t work. In Chapter 8, there is this simple script:
var levelToLoad : String;
var normalTexture : Texture2D;
var rollOverTexture : Texture2D;
var beep : AudioClip;
function OnMouseEnter(){
guiTexture.texture = rollOverTexture;
}
function OnMouseExit(){
guiTexture.texture = normalTexture;
}
function OnMouseUp(){
audio.PlayOneShot(beep);
yield new WaitForSeconds(0.35);
Application.LoadLevel(levelToLoad);
}
@script RequireComponent(AudioSource)
which should add an Audio Source component to an object, after the script is added. Well, it doesn´t work for some reason and I can´t find out why. Anyone knows what could cause it not to appear in the inspector ? :(
Thanks for help in advance !
Hmhmh no. I´ve seen that sometimes it works, sometimes not. I have created a new GameObject to test it and yea...didn´t work again.
In the situations where it didn't work, did it just fail to add the audio source, or did it fail to add the script as well?
I copied the code posted above and tested it a few times. It appears to work reliably. I'm not sure why it's not working for you.
Answer by Paul-Sinnett · Oct 25, 2012 at 12:48 PM
Did you already have the script attached before adding the RequireComponent line? I think it only adds the object at the point the script is applied to the object.
@Paul Sinneti that seems true. I had the same problem, detached the script, attach it again after adding the RequireComponent and it included the Audio Source
Your answer
Follow this Question
Related Questions
Does not work: @script RequireComponent(AudioSource) 0 Answers
What is the script code on how to trigger a song for the player to Listen to? 2 Answers
Assertion failure in AudioManager.cpp? 2 Answers
There is no 'AudioSource' attached to the 4 Answers
Multiple AudioSources overwriting a looping AudioSource 1 Answer