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
3
Question by Ilkzz · Jul 09, 2014 at 12:15 PM · c#audioaudiosource

Can not play a disabled audio source. Why randomly?

Hi,

I randomly started getting the error, but I have not changed anything within the codes. The sounds were working but it confusing as to why this would randomly happen.

I have set the variables as so:

     public AudioSource JumpSound;
     public AudioSource DieSound;
     public AudioSource FalldownSound;

and here is how I play them:

 void OnCollisionEnter2D(Collision2D other)
     {
         if (!hasPlayed)
         {
             DieSound.Play();
             hasPlayed = true;
         }
 
         if (!hasPlayed2)
         {
             FalldownSound.Play();
             hasPlayed2 = true;
         }
         Die ();
     }

What is the problem? I don't see why this will stop working randomly. I have checked all my audios and they're all ticked in the inspector.

EDIT:

This is the error message.

Can not play a disabled audio source UnityEngine.AudioSource:Play() UFO:OnCollisionEnter2D(Collision2D) (at Assets/Scripts/UFO.cs:35)

Line 35 is DieSound.Play();

The same error is seen for all 3 audio sounds I have

Comment
Add comment · Show 4
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 gjf · Jul 09, 2014 at 12:21 PM 0
Share

you'd be better off having in a single AudioSource and assigning different AudioClips to it.

also, when you get an error, try posting the complete error message so that the helpful folks on the forums/answers can get all of the info available and not have to guess at stuff...

help us help you ;)

avatar image Ilkzz · Jul 09, 2014 at 12:31 PM 0
Share

@gjf

I added the error message in the original question. I didn't know I can just copy and paste it!

How do I go about have more than audio clip to 1 audio source? The inspector only allows 1 clip?

avatar image Nerevar · Jul 09, 2014 at 12:42 PM 1
Share

I wonder how you assign your AudioSources if they are on the same object...

$$anonymous$$aybe try something like this :

 public AudioSource source;
 public AudioClip[] clips;

 
 
 void OnCollisionEnter2D(Collision2D other)
     {
         if (!hasPlayed)
         {
             source.clip = clips[0]; //DieClip
             source.Play();
             hasPlayed = true;
         }
  
         if (!hasPlayed2)
         {
             source.clip = clips[1]; //FalldownClip
             source.Play();
             hasPlayed2 = true;
         }
         Die ();
     }

or if you don't want an array of clips, use:

 public AudioClip JumpSound;
 public AudioClip DieSound;
 public AudioClip FalldownSound;
avatar image AndyMartin458 · Jul 09, 2014 at 05:35 PM 0
Share

Try looking in the inspector to make sure the sound is enabled.

8 Replies

· Add your reply
  • Sort: 
avatar image
8

Answer by alch_roy · Dec 20, 2015 at 03:08 AM

If you destroy the object before you are trying to play the sound you will get this error. an if(object != null) source.Play(); solved it for me

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
avatar image
1

Answer by DoctorWho · Jul 09, 2014 at 11:54 PM

click this to get right to everything audio

ok, if your audio source is disabled then it will not use the play function, look at your audio source it has a check box for being enabled or not, if its not checked it will not be updated unless you enable it with code. also inside it is a play on awake, this is what the code your using is acting on, and it to has a check box. there is a video all about sound made by a unity programmer in the online training archive hers that link : click : read or watch or both.

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
avatar image
1

Answer by Meet Jon Black · Jul 21, 2015 at 03:39 PM

If the issue does not have anything to do with the audio source being enabled, then you probably just need to Instantiate the AudioSource variables(JumpSound, DieSound and FalldownSound). If it is not in the actual game hierarchy, then theoretically it isn't enabled and it is just a file on your computer. I hope this helps anybody else with the same error, because it sure helped me.

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
avatar image
1

Answer by Drudail · Oct 15, 2017 at 05:57 PM

I had a similar issue, which was thay the object was being destroyed at the same time as the audiosource was supposed to play. I created a SoundController GameObject, unloaded all sounds to it and in script gave it the public AudioSource variables. In my GameController script I instantiated the SoundController and used the clips from there. Now whenever the object is destroyed the sound comes from a still-instantiated object so the sounds play. Bonus, it cleaned up a lot of other objects by unifying the location of all sound objects.

To make sure they play, the destroying script is also in GameManager and each object that hits passes in a strong to determine which sound to play.

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
avatar image
0

Answer by hjzhang · Sep 06, 2016 at 10:32 AM

Make sure you have checked the AudioSource Component.I just met the same mistake and I found I fogot to check the AudioSource Component.

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 MitchAlenPek123 · Mar 28, 2020 at 08:17 AM 0
Share

@alch_roy

Hello! I have had the same problem, although I am making a different kind of game and have different code for the audio. Please help!! I have my publics and privates set to:

 public AudioClip sound;
 
     private Button button {  get { return GetComponent<Button>(); } }
     private AudioSource source {  get { return GetComponent<AudioSource>(); } }

and my code is:

 void Start()
     {
         gameObject.AddComponent<AudioSource>();
         source.clip = sound;
         source.playOnAwake = false;
 
         button.onClick.AddListener(() => PlaySound());
     }
 
 
     void PlaySound()
     {
         source.PlayOneShot(sound);
     }
 }

I have my audio in the spot where unity asks for it, and yet it gives me the same error message. I have no problem playing the background audio, however.

Please help!!

  • 1
  • 2
  • ›

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

31 People are following this question.

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

Related Questions

How to play "audiosource" component in a prefab from script? 0 Answers

Triggering multiple audio clips to play in sequence 1 Answer

Help with sound clips [C#] 0 Answers

How to use "or" for Keycodes 2 Answers

This script opens up an Audio Source but what would I need to do to get it to open up a Resonance Audio Source instead? 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