- Home /
How do I get the enemies to damage the player?
It used to work. But then I swapped what I had for a Player to a FirstPersonController for menu reasons. Then I entered the main scene to find out that the enemies no longer damage the player.
using UnityEngine;
public class SendDamage : MonoBehaviour { public int damage;
private int setDamage;
private void Start()
{
damageOutput();
}
private void damageOutput()
{
setDamage = damage;
}
void OnCollisionStay(Collision other)
{
if (other.transform.CompareTag("Player"))
{
other.transform.GetComponent<PlayerDamage>().ApplyDamage(damage);
}
}
the Enemy SendDamage Script.
using UnityEngine; using UnityEngine.UI;
public class PlayerDamage : MonoBehaviour { public Text healthPanel; public int health = 100;
// Start is called before the first frame update
private void Start()
{
healthPanel.text = health.ToString();
}
public void ApplyDamage(int damage)
{
if (healthPanel != null && health > 0)
{
health = health - damage;
healthPanel.text = health.ToString();
}
}
}
and the Player Health script
Answer by WoppelPoppel · Apr 29, 2020 at 08:48 PM
I have a couple of ideas:
Does the new player have the "Player" tag?
Does the new player have the PlayerDamage-script?
Does the new player have a collision-hitbox (not just trigger!!)?
Does the new player have the collision and the PlayerDamage on the same object (not a child)?
Your answer
Follow this Question
Related Questions
Different Enemy Lives JavaScript 1 Answer
How to get health values into 3DTextMesh 1 Answer
How come my Player, Enemy, Animation & HealthSlider won't correspond to one another? 1 Answer
Enemy won't damage player C# 1 Answer
Fastest way to add calculate damage on different body areas (hit boxes) 1 Answer