- Home /
Cant Play Sound On Destroy
hi trying to play a sound on Destroy but it's not playing I don't know what I'm doing wrong really and not a lot I can explain
here's the code maybe you experts can help me, it could just be me not looking at it the write way I don't know
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Asteroids : MonoBehaviour
{
public float Torque;
public Rigidbody2D rb;
public GameObject asteroidLarge;
public GameObject asteroidSmall;
void Start()
{
float torque = (Torque);
rb.AddTorque(torque);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag ("bullet"))
{
Destroy(other.gameObject);
{
if (asteroidLarge);
Destroy(gameObject);
}
}
}
}
and here is the code for the SoundManagerScript
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SoundManagerScript : MonoBehaviour
{
public static AudioClip PlayerDeathSound, playerBulletSound, enemyDeathSound, AsteroidExplosion01, AsteroidExplosion02, AsteroidExplosion03;
static AudioSource audioSrc;
void Start()
{
PlayerDeathSound = Resources.Load<AudioClip>("PlayerDeath");
playerBulletSound = Resources.Load<AudioClip>("Fire");
enemyDeathSound = Resources.Load<AudioClip>("EnemyDeath");
AsteroidExplosion01 = Resources.Load<AudioClip>("Asteroid01");
AsteroidExplosion02 = Resources.Load<AudioClip>("Asteroid02");
audioSrc = GetComponent<AudioSource>();
}
void Update()
{
}
public static void PlaySound (string clip)
{
switch (clip)
{
case "Asteroid01":
audioSrc.PlayOneShot(AsteroidExplosion01);
break;
}
}
}
I've been working on this for since 6 am and its 8 pm now and I'm getting tired and I've still got to figure out how to play an animation after destroy too, I gave up on that I couldn't figure it out but 1 thing at a time
Regards Andy
Where do you call Sound$$anonymous$$anagerScript.PlaySound
?
Answer by sacredgeometry · Jan 28, 2020 at 11:42 PM
I am assuming you are destroying the gameobject with your sound manager script attached to it.
Long story short you should probably not be destroying anything in Unity. Destroy deallocates the gameobject and flags it for garbage collection.
Garbage collection causes performance dips so avoiding deallocation during gameplay is generally a good idea.
Try spawn pooling, which should fix your problem if your sound isn't directional. if it is you could hide.disable it and wait for the sound to finish before returning it to your spawn pool.
so I've not done a lot with pooling, and don't really know where to start, the way the game is set up is the asteroids get made invisible when they leave the game area I'm only destroying them when I shot them so there not a lot going on one there destroyed, but I could have my logic all wrong, what would the code look like with disabling a object and playing a sound, not only that I have to play an animation after it's destroyed too but I can't get any of it to work the way I want it too
Answer by adi7b9 · Jan 29, 2020 at 07:55 AM
void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag ("bullet"))
{
Destroy(other.gameObject);
{
//play the sound here
if (asteroidLarge);//this ";"
{
//play sound here
Destroy(gameObject);
}
}
}
}
Yes it is.
You can have code blocks whenever you want in a C# function. It just does not make much sense and could be simplified, but it's allowed.
Your answer
Follow this Question
Related Questions
How can I spawn different GameObjects using IEnumerator? 1 Answer
How do I know that an object is not coliding without using OnCollisionExit? 1 Answer
Collect pickup if mouse button is not pressed 0 Answers
Changing array of display images with other set of images on user button press event 0 Answers