Damage dealer effecting different tags

I’m having a little trouble with some functionality I want to add to my damage dealer script. When a projectile hits the player or enemy but doesn’t kill them I want it to trigger a smaller particle effect and sound to confirm that a hit has taken place. Since the same damagedealer script is on both projectiles I thought a good way to do this would be to check the tags of what was just damaged and then trigger the correct particle effect and sound for that game object. However I think I’m doing something wrong because it just isn’t doing anything. Here’s my code:

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

public class DamageDealer : MonoBehaviour
{
    [SerializeField] int damage = 100;

    [Header("Player hit")]
    [SerializeField] GameObject playerStrikeExplosion;
    [SerializeField] AudioClip playerStrikeSound;
    [SerializeField] float playerStrikeSoundVolume = .7f;
    [SerializeField] float playerExplosionDelay = 5f;


    [Header ("Enemy hit")]
    [SerializeField] GameObject enemyStrikeExplosion;
    [SerializeField] AudioClip enemyStrikeSound;
    [SerializeField] float enemyStrikeSoundVolume = .7f;
    [SerializeField] float explosionDelay = 5f;

    public int GetDamage()
    {
        return damage;
    }

    public void Hit()
    {
        if (tag == "Enemy")
        {
            Debug.Log ("The player has hit");
            GameObject explosion = Instantiate
        (enemyStrikeExplosion, transform.position, Quaternion.identity);
            Destroy(explosion, explosionDelay);
            AudioSource.PlayClipAtPoint(enemyStrikeSound, Camera.main.transform.position, enemyStrikeSoundVolume);
        }
        else if (tag == "Player")
        {
            Debug.Log("The enemy has hit");
            GameObject explosion = Instantiate
        (playerStrikeExplosion, transform.position, Quaternion.identity);
            Destroy(explosion, explosionDelay);
            AudioSource.PlayClipAtPoint(enemyStrikeSound, Camera.main.transform.position, enemyStrikeSoundVolume);
        }
        Destroy(gameObject);
    }
}

To try and figure it out myself I put debug messages in both if statements just to see if maybe the script was working but just the effects weren’t appearing. Unfortunately it doesn’t look like the script is triggering at all because neither messages showed up in the console. Any help would be really appreciated!

What calls Hit() and maybe try a print statement at the top of the method to make sure it’s getting called.

Hit gets called whenever an enemy or the player is hit. I tested that with a print statement and it’s working correctly with both interactions.

After taking another look at it, I actually figured it out! I just had to move that same script into the section of the player and enemy script where the hit was being processed. Glad it’s working now although I’m still curious as to why it didn’t work the first way.

Maybe it had something to do with which gameObject’s tag was getting compared? And by switching the script around it was looking at the right tags.

I bet that’s got something to do with it. I didn’t need to use the find tag lines on the new script because it was just directly on the enemy and player script, but maybe putting it on the damage dealer script the way i did made it not able to find the game object tags

Hi Bembrooks,

The problem is very likely that tag refers to the game object to which DamageDealer is assigned. To what game object is DamageDealer assigned?

Actually, there is no need to check for tags. You have a collision method somewhere. When the Enemy object gets hit, you decrease the health. In that method, you could check if the health is 0 or lower. If it is, you could call a public method on the laser which makes the laser instantiate an explosion particle system at its position.

That’s essentially what I did, just more broadly applied I guess. I just put the instantiated particle effect and sound clip onto the onTriggerEnter2D section of the enemy and player code and that did the trick. It still plays when the enemy or player dies but the death sound and particle effect overpower the hit ones so it doesn’t make much of a difference.

Does that mean you solved the problem? :slight_smile:


See also:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms