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!