.Play() function not working?

So,
I’ve made a script called DestroyBySword that i’d like to be able to drop on any object that will be able to be destroyed by a sword.
I’d like the following to happen when the sword Collider(Tagged “Sword”) enters the collider on the object.
1.Play a sound
2.Play a burst particle system for whatever the debris looks like for that object
and finally
3.Destroy the object.
In this scenario I’ve made a clump of grass object with the DestroyBySword script attached
I’ve also attached a audio source to the object
I’ve also parented a particle system that bursts some grass looking particles.
My script allows for dropping in an audio source and a particle system.
I did this for the flexibility of using this one script for all destructible objects in my game.
Here is my script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DestroyBySword : MonoBehaviour
{
public AudioSource ImpactSound;
public ParticleSystem Debris;

void Start()
{
ImpactSound = GetComponent();
Debris = GetComponent();
}

void Update()
{

}
void OnTriggerEnter(Collider other)
{

if (other.gameObject.tag == “Sword”)

Debris.Play();
ImpactSound.Play();
Destroy(this.gameObject);
}
}

The object destroys but neither the audio nor the particle system plays.
What am I doing wrong here?

If your audiosource and particle system are attached to the gameobject you are destroying, that would explain why they aren’t playing (since you are destroying them).

My go to way of approaching this is to instantiate a separate object that you attach all the effects, such as particle systems, point lights, sounds, etc, that you want played when something is destroyed. Have all the effects set to play on awake, or have a separate script on that effects object that kicks them all off on start. So you instantiate that effects object, tell it to destroy itself in like 3 seconds, then tell the gameobject your script is on to destroy itself now. Use stuff like object pooling to improve efficiency if you are doing this all the time.

1 Like

Okay, yeah. It makes sense that i’m destroying the things i’m wanting to play.
I wasn’t sure how unity handled this.
Thanks so much for the quick reply.
I’ve got it working like a charm now.