- Home /
How to convert @script RequireComponent(AudioSource) to C#
I get the error "MissingComponentException: There is no 'AudioSource' attached .." How do I convert javascript's @script RequireComponent(AudioSource) to C#?
Thanks, Jeff
Answer by fireDude67 · Jan 08, 2011 at 04:25 AM
using UnityEngine;
[RequireComponent(typeof(AudioSource))] [RequireComponent("AudioSource")] // Other way public class MyScript : MonoBehaviour { // etc..
I read your comment and to add an AudioSource
to your GameObject
simply drag an Audio file onto the Object
Answer by Eric5h5 · Jul 26, 2010 at 09:31 PM
I did have this attribute in my code, but I am getting the error when I run the script during game play.
@Jeff: if you added the "RequireComponent" line after the script was already attached to an object, it won't do anything. It only works when attaching a script to an object; it's not something that happens at run-time. But you can simply manually add the component yourself anyway. RequireComponent is just there to be used with pre-made scripts, in order to make things more bullet-proof when sharing the script with others.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to get C# Array to Load into Audio Source 0 Answers
AudioClip stuttering while loop is OFF and not using function Update? 1 Answer
Change AudioSource output via Script 1 Answer