- Home /
OnDisable() not working in case of audioclip.
I am trying to play an audio clip after the gameobject has been SetActive(false) that means after it is disabled. But I am not able to get any result. Audio clip is not playing after the gameobject has been disabled. Give me a solution.
Answer by AshAbeAdd · Jun 16, 2017 at 06:59 AM
How exactly are you doing this? Do you have public audioscripts attached to the script? Where do you have this script attach to, something that is permanent or temporary? For example if you have a main menu script, you could put in a public audio audioname; Set several different methods that get called in the script. Then in the section that you setactive(false) you can call a method to start the audio. However this all depend on how you're doing this. Make sure you're not disabling the script before you are calling the audio. Also make sure that the audio is being summoned. Put a Debug.Log(Audiotest); in there. However you need to make a bool for that and some codes.
my code is as follows:
if(box_count == 5){ GetComponent().PlayOneShot(enemy_death); enemy.SetActive(false); }
What about your audio? Is it in your code? There's a few things that I could say about the way you written your codes so far. I find it safer to not include symbols for variable, unless it's needed. Such as how namespace and hashtags are used. I'm a bit concerned about how you're using GetComponent() as well. You should read the definition and examples given by Unity. Last, I would recommend just activating and doing a check test on the audio within that if statement.
After the failure of previous script I tried this one:
if(box_count == 5){ enemy.SetActive(false); }
void OnDisable(){ GetComponent().PlayOneShot(enemy_death); }
where enemy_death is a public AudioClip and enemy is the public GameObject to which the script is attached.
but from this also I didn't get any fruitful result.
I'm not sure what it's called in Unity, but I believe your prototype is incorrect. If you're doing a 'public GameObject audioName;' Then that's the main error. Unless of course you have an audio attached to a Gameobject. The second word in this syntax is all about the type of value that you're adding. A gameobject would be anything such as a shape or a model. I believe audio is what you need for audio. You would have Texture, Image, and so on for the various items that you're trying to alter in the script that you're working in that body. In your new set of codes, I don't see a connection from the if statement to the OnDisable method. Plus it's a very horrible way of doing that, because you might want to disable other values.
Could you suggest me a better way of adding an audioclip after disabling the gameobject?
I decided to look back at one of my program and see what I had written for the audio source. We should make sure that you're actually playing the audio normally before putting it in a slightly more complicated location. Considering how hard it can be to find an actual pseudo for audio, I'll just give you one. AudioSource.PlayClipAtPoint(AudioFile, position); Insert your audio in the AudioFile and then you'll have several different options for position. I would recommend just using transform.position. Put this script in the Start() to see that you are loading in the audio.
Your answer
![](https://koobas.hobune.stream/wayback/20220612124257im_/https://answers.unity.com/themes/thub/images/avi.jpg)