Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

getComponent<Audio Source>().Play(); not calling up my sound file despite there being no errors

Discussion in 'Scripting' started by Lethn, Aug 6, 2015.

  1. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class VegetablePickup : MonoBehaviour {
    5.  
    6.     public int pointsToAdd;
    7.     public AudioSource eaten;
    8.  
    9.  
    10.  
    11.     void OnTriggerEnter2D ( Collider2D other )
    12.  
    13.     {
    14.  
    15.         if (other.GetComponent<PlayerController>() == null)
    16.             return;
    17.         ScoreManager.AddPoints( pointsToAdd );
    18.         gameObject.GetComponent<AudioSource> ().Play ();
    19.  
    20.         Destroy (gameObject);
    21.     }
    22. }
    I've been following a tutorial and yet again the code itself makes total sense but I clearly haven't either placed it somewhere correctly or I've just not noticed something again. After trying lots of combinations of various suggestions out there I've given up and posted here as I can't find anything matching my exact problem.

    What happens is that my sound will play absolutely fine by itself, but when trying to get it to play when my player enters the collider it simply doesn't want to work. The only error I've been able to replicate is by move the position of the GetComponent function which triggers the warning "Can not play disabled audio source".

    This is a problem as in all tutorials associated with this little bit of code they tell you to turn off Play on Awake and once you turn it back on it just plays automatically and doesn't react at all to the code ( pretty obnoxious of the damn compiler if you ask me :p ) now I also tried setting the play on awake true in the actual code but the compiler didn't like that either and threw up errors.

    So, long story short, I'm stuck now, I'd appreciate the help, in the tutorials they used audio.Play(); rather than getComponent ( or in my case eaten.Play(); ) but I read around and apparently that command has become obselete now and replaced with what I've used above.
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,392
    audio.Play() and GetComponent<AudioSource>().Play() are essentially the same thing.

    • Could it be that its working but the GameObject is too far from the Camera for you to hear the sound?
    • Is OnTriggerEnter2D actually being called? Put a Debug.Log statement inside to make sure.
    • Have you added the VegetablePickup component to the trigger component?
    • Is the collider set as a trigger?
    • Is the Audio Source on the same object as the collider and the VegetablePickup component?
    • Are you using a 2D collider?
     
  3. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    wow that was fast lol, will check all of those things, also, it shouldn't be too far, I've made it a 2D sound precisely for testing purposes and I can hear it playing absolutely fine when I have the audiosource playing normally. Collider is definitely set as a trigger, I've attached the audio source to an already existing object which has a working collider on it. The script is naturally on the same object as well.

    This is your classic pickup, you walk into it and the object gets destroyed, I was in the process of finishing it all but then ran into this annoyance.

    Oh as for the audio.Play I thought that was for an older version of unity and obselete.
     
  4. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,392
    What do you mean you attached the audio source to an existing object? The sound wont play unless its on the same object as your script and collider, thats how your code works.
     
  5. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Sorry I worded that badly, what you said is what I've done, I put the audio source, the script and the collider all on the same object and the audio source is connected to the script through the public AudioSource function.

    Maybe it would help if I showed the tutorial I was following.



    It all makes perfect sense so I'm just trying to figure out where I've screwed it up, keep thinking there's a typo somewhere or a capitalisation problem because that's usually the culprit with most of my problems.

    The bit of code I'm using is just at the end with the coinpickups, at about 19:40, as I said, the only error I can get is "Can not play a disabled audio source", it happens only when PlayOnAwake is unchecked in the inspector.
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,392
    Code (csharp):
    1. gameObject.GetComponent<AudioSource>().Play();
    Will play an audio source on the current object.

    You want
    Code (csharp):
    1. eaten.Play();
    I missed that in your original post :eek:
     
  7. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    That was the first one I tried, I have been able to replicate the warning error now though.

     
  8. Manny Calavera

    Manny Calavera

    Joined:
    Oct 19, 2011
    Posts:
    205
    You are destroying the object soon after calling Play(). So there isn't enough time for the sound to be played.
     
    xAmrxxxx likes this.
  9. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Hmmm, that doesn't explain the warning I'm getting though.
     
  10. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,392
    You probably disabled the audio source. Enable it in the inspector or in code.
    eaten.enabled = true;
     
  11. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Well that's interesting, that got rid of the error, but the sound still doesn't play. By the way, like I said, with the Play On Awake checked in the inspector it works but it just plays by itself and completely ignores the script despite everything being on the same object.
     
    Last edited: Aug 7, 2015
  12. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    did you check if that oncollision even runs? because eaten.Play() should play the sound if correct one is set in audio source. also try without destroying gameobject
     
  13. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    I'm getting somewhere now thanks to that suggestion Phoda! It looks as if somehow the Destroy function is interfering entirely with the process. The only time I can get the audio working correctly is if I remove Destroy out of the code entirely but then that of course means that the whole item pickup process isn't working correctly and the sprite is still there.

    So that means, so far, I can get the two functions to work seperately but not at the same time, because one is interfering with the other and I think Destroy is the culprit. As for the onCollision it must be working because it does absolutely fine without the audio code.

    Do you think it could possibly be because Destroy is simply located all in the same code and is destroying the sound immediately regardless of where it's placed? That's what I'm thinking, I would think there would be a way to white list the sound for a certain amount of time or perhaps I should put in an entirely seperate empty with a collider and see how that works?

    Edit: OKAY! creating an entirely seperate empty and placing the audio code on it makes everything work fine, however the moment I try parenting this empty to the object that's being destroyed it stops working again.

    This is very incovenient though as the empty is going to stay there no matter what so that means the player can end up triggering it if they decide to go back there which isn't what you want happening with an object the player is picking up.
     
    Last edited: Aug 7, 2015
  14. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    I had a similar issue & spent hours on it before I worked out that the sound was playing but I was destroying the object the line after so I never actually got to hear it. I'm ended up having a sound manager on an empty object in the scene & just told it to play the sounds as I continued to destroy the other game objects.

    Edit: I originally had the sound in the object that was being destroyed.
     
  15. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Would you mind providing some example code so I can set it up? That sounds a lot like an empty LevelManager script I'm using from the tutorials for checkpoints and so on.
     
  16. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    Sorry, I don't have access to my PC atm, but yes I did end up using the empty object as the game manager as it was easier to get it to handle all that stuff like sound, scores, lives etc. from memory I just passed an int across & each int mapped to a different sound
     
  17. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Hmmm well regardless I'll experiment with that and see, thanks, it makes sense though, because like you say all that's happening is the destroy function is indiscriminately killing off the sound before it plays which must be why the disabled message comes up.

    It's been disabled before it's even had a chance to run, there should be a simpler way of doing this though it's only an audio file, is there a way to just stop the destroy object from destroying the audio file for a specific length of time? I know I could do it on the whole object but then it would just look wrong when the player runs into it.
     
  18. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    I don't think there is. My teacher explained it as being like everything attached to the game object is reliant on it existing so when it is destroyed everything goes with it. Once that happens things attached to it like sounds no longer exist
     
  19. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,392
    Unparent the audio before you destroy the gameobject then destroy the audio with a delay

    Code (csharp):
    1. eaten.transform.parent = null;
    2. eaten.Play();
    3. Destroy(eaten.gameObject, eaten.clip.length);
    4. Destroy(gameObject);
     
    Lethn likes this.
  20. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
  21. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    would not be best if you made sound manager and play sounds from there? or destroy object after sound.
     
  22. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    I tried the delayed destroy, exact same problem, even when unparented the destroy function interferes with the audiosource.

    Well I was suggested that but I don't know how to implement it properly, I tried messing around with the PlayClipAtPoint function yesterday when I was searching for solutions but it didn't go well.,
     
  23. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,392
    Is your audiosource on the same object as the script? That would explain the issue you had.

    Do this instead:
    Code (csharp):
    1. Destroy(gameObject,eaten.clip.length);
     
  24. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    Yes it is, do I need to put in the name of the actual clip and have it play outside as a prefab then is that what you're saying? That bit of code is entirely new to me so I'll need to experiment.
     
  25. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    IT WORKED!!!!

    It looks like it was what you said karl.jones, what I did was I put the audio source on a seperate empty and then had the script on the actual object with the destroy delay you implemented, now everything seems to be working fine even with the parenting.

    Holy crap that was unnecessarily annoying, so why does the simple code I used first work on gamesplusjames' tutorial but not on my code? Is it something to do with the Unity versions? That's what I'm curious about now because he didn't need to do all of this.
     
    Last edited: Aug 7, 2015
  26. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    its not unity versions, probably you missed some small detail that was important, dont worry and learn refferencing other scripts ASAP as managers are one of the easiest ways to, well, manage things
     
  27. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,392
    The way he does it is fine, my guess is that you have put the audio source on the coin and not the parent gameobject like he does in the tutorial.
     
  28. Lethn

    Lethn

    Joined:
    May 18, 2015
    Posts:
    1,583
    I totally missed that lol I'm going to go back and double check, it must be because I've got a cold and I'm on drugs this week but that was ridiculous even by my standards.

    Anyway, thanks for your help guys, it's all working great now.

    Yes, he mentions it very briefly but he's got an empty and what he's done is he's parented all the coins to the one empty that contains the audio source.....

    *repeatedly bashes head against desk*

    Spent a whole bloody day on this lol, how the hell can this be more difficult than Navmesh agent programming?
     
    Last edited: Aug 7, 2015
    karl_jones likes this.