using UnityEngine;
using System.Collections;
public class VegetablePickup : MonoBehaviour {
public int pointsToAdd;
public AudioSource eaten;
void OnTriggerEnter2D ( Collider2D other )
{
if (other.GetComponent<PlayerController>() == null)
return;
ScoreManager.AddPoints( pointsToAdd );
gameObject.GetComponent<AudioSource> ().Play ();
Destroy (gameObject);
}
}
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 ) 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.
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.
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.
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.
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.
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
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.
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.
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.
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
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.
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