1
\$\begingroup\$

I have this damage script and I want it to instantiate a particle system...a death effect on enemy destroy. How do i add that to this script, please?

using UnityEngine;
using System.Collections;
using UltraReal.Utilities;
using UltraReal.WeaponSystem;

public class BasicDamage : UltraRealDamageBase
{

    /// <summary> 
    /// Reference to a damage sound effect
    /// </summary>
    [SerializeField]
    protected AudioClip _damagedSound = null;

    /// <summary> 
    /// Reference to a death sound effect
    /// </summary>
    [SerializeField]
    protected AudioClip _deathSound = null;

    /// <summary> 
    /// Reference to Audio Source. If none is provide, it will try to get the one on the game object.
    /// </summary>
    [SerializeField]
    protected AudioSource _audioSource = null;

    /// <summary> 
    /// Overriden OnStart method from UltraRealDamageBase.
    /// </summary>
    protected override void OnStart()
    {
        base.OnStart();

        _audioSource = GetComponent<AudioSource>();
    }

    /// <summary> 
    /// Overriden OnDeath method from UltraRealDamageBase.
    /// </summary>
    protected override void OnDeath()
    {
        base.OnDeath();

        if (_audioSource != null && _damagedSound != null)
            _audioSource.PlayOneShot(_deathSound);

        Destroy(gameObject);
    }

    /// <summary> 
    /// Overriden OnDamaged method from UltraRealDamageBase.
    /// </summary>
    protected override void OnDamaged(float damage)
    {
        base.OnDamaged(damage);

        if (_audioSource != null && _damagedSound != null)
            _audioSource.PlayOneShot(_damagedSound);
    }
}
\$\endgroup\$
  • \$\begingroup\$ It appears like you are using some framework or 3rd party asset which defines an OnDeath method which apparently gets called when something dies. Is that method being executed and is that where you would like to spawn the particle effect? Spawning something new in the default OnDestroy method is something I would avoid, because there are situations where you want something to disappear without making a fuzz about it (for example when switching the scene). \$\endgroup\$ – Philipp Mar 15 '17 at 14:52
1
\$\begingroup\$

Add the ParticleSystem that you want to play to your script, just like you did for the AudioClips. Then you could use something like this in the OnDeath method to instantiate it and play it once:

protected void PlayEffectOnce(ParticleSystem prefab, Vector3 position) {
    if (prefab == null) { return null; }
    ParticleSystem ps = Instantiate(prefab, position, Quaternion.identity) as ParticleSystem;
    Destroy(ps.gameObject, ps.startLifeTime);
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.