When I shoot the projectile at the Enemy in testing (lesson 103); it is hitting the enemy, however the Health is not decreasing. Enemy has Circle Collider 2D component, EnemyPathing.cs, Enemy.cs.
Hi monan,
Welcome to our community!
Are there any error messages in your console when the health is supposed to decrease?
No errors.
The missiles are deflected from enemy, as if untouched. They bounce off all over.
Check your code. Bouncing off sounds as if two non-trigger colliders are involved. If you use the OnTriggerEnter2D method in your code, it won’t get called because there must be at least one trigger collider involved in the collision event to make it get called.
Hi Nina,
I am having a similar problem. Lasers passing straight through with no reduction in health. Everything looks setup exactly the same Rick’s code and Unity settings:
Enemy.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
[SerializeField] float health = 100;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerEnter2D(Collision2D other)
{
DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
health -= damageDealer.GetDamage();
}
}
DamageDealer.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DamageDealer : MonoBehaviour
{
[SerializeField] int damage = 100;
public int GetDamage()
{
return damage;
}
public void Hit()
{
Destroy(gameObject);
}
}
Hi @Dylan_Fryer,
Try to fix the first error message first. It states that the parameter of the OnTriggerEnter2D method must be of type Collider2D. Make sure that your code uses this type and not another one.
Regarding the other two errors, NullReferenceException means that a reference (“link”) to an instance is missing. Double click on the error message to see to which line in your code it is referring. If you exposed a field in the Inspector, make sure that it’s not empty.
See also:
- Forum User Guides : How to apply code formatting within your post
This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.