I’m working on the Laser Defender tutorial (video 106: Player Life and Death) and everything was going great until last night when I copied and pasted the OnTriggerEnter2D and ProcessHit methods from the enemy to the player script. The enemies take damage and are destroyed when hit by the player but the player isn’t taking any damage from the enemy projectiles. I have Debub.Log() everywhere and I’ve narrowed it down to the damageDealer variable in Player.cs which is returning Null but I don’t understand why that is. Before editing the collision layers, the player could kill itself so only lasers coming from the enemies are null. I tried giving the same laser prefab to the enemies but that didn’t work either. It all worked for Rick right away so I must have missed something. I’ve combed through everything over and over for about 2 hours and I can’t figure this out. I could really use some help, thanks in advance.
This is my first post, sorry if I do this wrong. Hopefully, this will be enough, going to paste enemy.cs and relevant parts of player.cs. Also some screenshots of my console output and prefabs.
Player.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
[Header(“Player”)]
[SerializeField] float moveSpeed = 10f;
[SerializeField] float padding = 1f;
[SerializeField] int health = 100;
[Header("Projectile")]
[SerializeField] GameObject playerLaser;
[SerializeField] float projectileSpeed = 15f;
[SerializeField] float projectileFiringPeriod = 0.1f;
Coroutine firingCoroutine;
float xMin;
float xMax;
float yMin;
float yMax;
// Start is called before the first frame update
void Start()
{
setUpMoveBoudaries();
}
// Update is called once per frame
void Update()
{
Move();
Fire();
}
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("Player hit!");
Debug.Log("Other: " + other);
DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
Debug.Log(damageDealer); // THIS LINE IS ONLY RETURNING NULL.
ProcessHit(damageDealer);
}
private void ProcessHit(DamageDealer damageDealer)
{
Debug.Log("Processing player hit...");
health -= damageDealer.GetDamage();
Debug.Log("Health: " + health);
if( health <= 0 )
{
Destroy(gameObject);
}
}
Enemy.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
[SerializeField] float health = 100;
[Header("Enemy Projectiles")]
[SerializeField] float shotCounter;
[SerializeField] float minTimeBetweenShots = 1f;
[SerializeField] float maxTimeBetweenShots = 2f;
// Enemy projectile
[SerializeField] GameObject enemyProjectile;
[SerializeField] float projectileSpeed = 10f;
// Start is called before the first frame update
void Start()
{
shotCounter = Random.Range(minTimeBetweenShots, maxTimeBetweenShots);
}
// Update is called once per frame
void Update()
{
CountDownAndShoot();
}
private void CountDownAndShoot()
{
shotCounter -= Time.deltaTime;
if(shotCounter <= 0)
{
Fire();
shotCounter = Random.Range(minTimeBetweenShots, maxTimeBetweenShots);
}
}
private void Fire()
{
GameObject laser = Instantiate(enemyProjectile, transform.position, Quaternion.identity) as GameObject;
laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, -projectileSpeed);
}
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("Enemy hit! " + other.gameObject.name);
DamageDealer damageDealer = other.gameObject.GetComponent<DamageDealer>();
Debug.Log("Enemy script damage dealer: " + damageDealer);
ProcessHit(damageDealer);
}
private void ProcessHit(DamageDealer damageDealer)
{
Debug.Log("Processing enemy hit.");
health -= damageDealer.GetDamage();
Debug.Log("Enemy health: " + health);
if( health <= 0 )
{
Destroy(gameObject);
}
}
}
![console_player_hit|566x484](upload://feIoRO5daEDxHURKxfRBXyvTUjt.png)
![laserRed06|188x499](upload://iXHOoL3zHx0CtqmAH0I4dh60Zt4.png)
![playerPrefab|188x500](upload://iqm1gMIunOwmejImGfVxDalfGha.png)