How I solved The Bugs in "Player Life And Death"

Hello Kind Sir/Mam,
I have been following this course for a while as a way to learn new things.
I always wanted to learn game development as a hobby but never knew where and how to start, until I stumbled upon this course.
I am completely new to C# and unity but have a beginner level of experience in C and C++ from college days but I am a clean slate now​:joy::rofl: as it has been 5 years since I opened an IDE.
But Thank you, Rick, Nina, Kevin-Brandon, Ben, and others from this amazing community.
I think my code might be filled with rookie mistakes and will be sore for an experienced programmer eye. But was so happy I can’t wait to share my code with you.

In Player.cs
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag(“Enemy Lazer”) || other.gameObject.CompareTag(“Enemy”))
{
DamageDealer damageDealer = other.GetComponent();
ProcessHit(other, damageDealer);
}
}

private void ProcessHit(Collider2D other, DamageDealer damageDealer)
{
    playerHealth -= damageDealer.GetDamage();
    if (other.gameObject.CompareTag("Enemy Lazer"))
    {
        Destroy(other.gameObject);
    }
    if (playerHealth <= 0)
    {
        Destroy(gameObject);
    }
}

And from Enemy.cs
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag(“Player Bullet”) || other.gameObject.CompareTag(“Player”))
{
DamageDealer damageDealer = other.gameObject.GetComponent();
ProcessHit(other, damageDealer);
}
}

private void ProcessHit(Collider2D other, DamageDealer damageDealer)
{
    enemyHealth -= damageDealer.GetDamage();
    if (other.gameObject.CompareTag("Player Bullet"))
    {
        Destroy(other.gameObject);
    }
    if (enemyHealth <= 0)
    {
        Destroy(gameObject);
    }
}

}

Can you please tell me the problem with my code that will harm my game in the future?
Thank you

1 Like

I’m so happy you shared this and are enjoying the course!

Privacy & Terms