Question by
Z1cro · Dec 09, 2020 at 11:23 PM ·
triggerrigidbody2dcollider2ddamagehealth
Can't get health/damage to work
Heyllo, I'm new to C# and can't figure out why my Script isn't working. 1. Yes, my player is on the "Player" tab 2. The collider for the enemy is set to "trigger" 3. On collision there's no debug log, or error message, it just doesn't work for some reason.
Any help would be GREATLY appreciated, here's my script(s)...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Obstacle : MonoBehaviour{
public int damage = 1;
void OnTriggetEnter2D(Collider2D other)
{
if (other.CompareTag("Player")) {
other.GetComponent<Health>().health -= damage;
Debug.Log(other.GetComponent<Health>(). health);
Destroy(gameObject);
}
}
}
Health:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Health : MonoBehaviour{
public int health = 3;
private void Update(){
if (health <= 0) {
Destroy(gameObject);}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
}
If all this seems good and the problem is most likely in Unity please let me know. Thank you.
Comment