Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Jeff 3 · Jul 27, 2010 at 07:18 PM · missingcomponentexception

I get the error "MissingComponentException: There is no 'AudioSource' attached .."

I am new to Unity. I am going through 'Unity Game Development Essentials'. I have written code in VB.NET and so I think I would be more comfortable writing scripts in C#. I try converting one of the Javascripts in the book to C#. When I run the script in game mode I get the error "MissingComponentException: There is no 'AudioSource' attached ..". I did post this issue on another thread but as an unregister user. So I thought I would post as a register user. I listed my code below. What am I doing wrong?

using UnityEngine; using System.Collections;

[RequireComponent(typeof(AudioSource))]

public class InstructionBtn: MonoBehaviour {

 public Texture2D normalTexture;
 public Texture2D rollOverTexture;
 public AudioClip beep;

 public string levelToLoad;


 // Use this for initialization
 void Start () {

 }

 // Update is called once per frame
 void Update () {

 }

void OnMouseEnter() {
guiTexture.texture = rollOverTexture;
//yield return new WaitForSeconds(.01); }

 void OnMouseExit() {
     guiTexture.texture = normalTexture;
 }

 IEnumerator OnMouseUp() {
     audio.PlayOneShot(beep);
     //StartCoroutine(Yielding(.35f));
     yield return new WaitForSeconds(.35f);
     Application.LoadLevel(levelToLoad);
 }
 private IEnumerator Yielding(float sec)
 {
     yield return new WaitForSeconds(sec);
 }
 private IEnumerator Yielding1(float sec)
 {
     yield return new WaitForSeconds(sec);
 }

}

Thanks, Jeff

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Mike 3 · Jul 27, 2010 at 07:31 PM

That actually looks perfect to me - absolutely sure that it's this script that is the problem? If so, have you tried reimporting it?

The RequireComponent attribute up at the top should be adding the audio source automatically

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jeff 3 · Jul 28, 2010 at 12:33 PM 0
Share

I am not sure what you mean by reimporting. However, the code was short so I deleted the script and created a new script with the same code and now it works. Hmmm...that is weird?!?!?

avatar image Mike 3 · Jul 28, 2010 at 01:12 PM 0
Share

Reimport is right click script > reimport. But no, not weird - recreating the script would do the same thing.

avatar image
0

Answer by Kristen · Jul 27, 2010 at 07:32 PM

In your code you wrote :

[RequireComponent(typeof(AudioSource))]

And so that means the object that this code is attached to must have a component of an audio source as well in order for it to work.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mike 3 · Jul 27, 2010 at 07:40 PM 2
Share

That tells unity that one is required, so it needs to be added by the engine when the script is initialized if one doesn't already exist

avatar image
0

Answer by roberto_sc · Mar 13, 2017 at 05:56 AM

It adds the required component when you attach the script to the object, so if you attach it before you wrote the [RequireComponent] bit, it won't work.

You'd have to reset the script, or remove it and add again.

Or, you can stop using the RequiredComponent attribute and add it when using:

 var audioSource = GetComponent<AudioSource>();
 if(audioSource == null)
     audioSource == gameObject.AddComponent<AudioSource>();

Please notice this is not the same thing, since it adds when using, if necessary. An alternative is to add it in the Start method, so it would have a similar behaviour as RequiredComponent.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

MissingComponentException after changing Animator component. 1 Answer

Missing Component Exception on light, which is there 1 Answer

change the wrap mode of a 2d animation 0 Answers

Missing Component Exception Error 1 Answer

GUItexture help me 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges