Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 ArunChnadran · Jan 23, 2012 at 11:14 AM · audioaudiosource

There is no 'AudioSource' attached to the

I'm pretty new to unity as well as game programing and other stuffs. I am getting this error " There is no 'AudioSource' attached to the "Character" game object, but a script is trying to access it." Here goes my code.

 function OnCollisionEnter(col : Collision)
 {
 
   if(col.gameObject.tag == "bomb")
   {
      // I got hit by a bomb!  
     Instantiate(explosion, col.gameObject.transform.position, Quaternion.identity);
     audio.PlayOneShot(explosionSFX);
   } 
   else if (col.gameObject.tag == "stein")
   {
     animation.Play("catch"); 
     audio.PlayOneShot(catchSFX);
   }
   col.gameObject.transform.position.y = 50;
   col.gameObject.transform.position.z = -16;
   col.gameObject.transform.position.x = Random.Range(0,60);
 }

Dragged and dropped the audio source from my folder in project panel, (tried it in prefab, Player game object in hierarchy and in code). nothing gives me desired the output. Can any one please help me?

Comment
Add comment · Show 3
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 syclamoth · Jan 23, 2012 at 11:17 AM 0
Share

So, you have an AudioSource component on the same gameObject as this script?

avatar image ArunChnadran · Jan 23, 2012 at 12:22 PM 0
Share

Not actually a component. I've declared them in the beginning of the script and they were available for me in the inspector panel.

avatar image syclamoth · Jan 23, 2012 at 12:37 PM 0
Share

Well, that explains it.

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by syclamoth · Jan 23, 2012 at 12:37 PM

You need to add an AudioSource component to the object. Keep in mind that if you declare

 var audio : AudioSource;

it will hide the Component.audio lookup variable, which finds and returns any AudioSource component that is attached to the same GameObject.

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 ArunChnadran · Jan 24, 2012 at 07:04 AM 0
Share

any way, that worked!!! Thanx a lot!!

avatar image
0

Answer by ArunChnadran · Jan 24, 2012 at 05:39 AM

But I got this working without adding any audio component.

 var speed:int;
 var audioClips:AudioClip[];
 
 //some functions
 function Explode(pos:Vector3)
 {
     audio.PlayOneShot(audioClips[Random.Range(0, audioClips.length)]);
     Instantiate(explosion, pos, Quaternion.identity);
 }

for this, I just dragged and dropped the files in to the gameObject and pressed apply(for its a prefab) and I could here different sounds.

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 syclamoth · Jan 24, 2012 at 05:49 AM 0
Share

Well, that's odd. 'audio' should return null if there is no AudioSource attached to the same object.

avatar image
0

Answer by karl_ · Jan 24, 2012 at 05:56 AM

Add this line to your script above the Class declaration:

[RequireComponent(typeof(AudioSource))]

or in JS

@script RequireComponent(AudioSource)

This automatically adds the Component you specify. Alternatively, you could just add the component manually. (Component/Audio/Audio Source). From here, calling audio.Whatever() will automatically get the AudioSource component for you. Now you can load up clips to the AudioSource like this:

@script RequireComponent(AudioSource)

var myClip : AudioClip;

function Start() { audio.clip = myClip; // Sets the audio source's current clip to 'myClip' audio.Play(); // Actually plays the clip.

}

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 ArunChnadran · Jan 24, 2012 at 07:05 AM 0
Share

mate this method gave me the same error! But I whole heartedly appreciate your help and expect them in future! thanx!

avatar image
0

Answer by venkspower · Jan 24, 2012 at 07:11 AM

Otherwise, make a GameObject. Add an AudioSource, and import the required audio file to this GameObject (which should be of .wav or .mp3 format). And attach this GameObject to the script. And activate this GameObject, whenever required. This should work fine.

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 ArunChnadran · Jan 24, 2012 at 01:47 PM 0
Share

thanx for the new tips venks!

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Playing each channel of a midi track on separate audio sources? 0 Answers

Audio on object plays low in unity 5 1 Answer

Stopping audio source without stopping oneshot clips 0 Answers

Unity Audio Output Data 0 Answers

Muting music at the start of the game. 1 Answer


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