About 'DamageDealer.cs'

in this code we mention this:

public void Hit()
    {
        Destroy(gameObject);
    }

but when the laser collides with the enemy it doesn’t get destroyed!
even though it’s “isTrigger”

does it have to like that or there’s something missing?

because the project works great so far, but this somehow feels that it doesn’t make sense :sweat_smile:

Not quite sure what you mean there Saleh?

If you have the isTrigger set to true, then you’d need a corresponding method such as;

private void OnTriggerEnter2D(Collider2D collision)
{
    // ...
}

If you take a look at the Enemy.cs script you can see this is in there, it subsequently calls the ProcessHit method, which gets a reference to the DamageDealer class and finally calls the Hit method.

It isn’t one of the best examples of good code architecture in my opinion, but, coupled together in the way shown in the course will work. I think there are some issues with the names that have been used for both classes, methods and variables in many of these cases which can cause a little confusion.

Currently, whatever has the DamageDealer.cs script component to it can be destroyed by calling the Hit method, this includes the enemy laser prefab, the player laser prefab, the enemy, and the player.

Do you have the DamagerDealer.cs script as a component of your laser(s)?

1 Like

yes the script is attached to the laser.
I was preferring to the laser getting destroyed upon hitting the enemy

here’s the full code:


public class DamageDealer : MonoBehaviour {

    [SerializeField] int damage;

    public int GetDamage()
    {
        return damage;
    }

    public void Hit()
    {
        Destroy(gameObject);
    }
}

and for the enemy it does get destroyed so no problem with that :slight_smile:

Hi,

Sorry, I’m still unclear as to what the problem is. Are you saying that in your game, the enemy is getting destroyed, but the player’s laser isn’t, when it hits the enemy?

yes

Thanks for clarifying.

So, you should have a method within your Enemy.cs script that looks like this;

private void OnTriggerEnter2D(Collider2D collision)
{
	DamageDealer damageDealer = collision.gameObject.GetComponent<DamageDealer>();

	if (!damageDealer)
	{
		return;
	}

	ProcessHit(damageDealer);
}

When there is a collision here, the first line gets a reference to the DamageDealer component on the other GameObject, e.g. the thing that just hit the enemy. In this case, this would be the player’s laser.

Assuming it finds this component, it calls ProcessHit and passes that instance of the DamageDealer class as a reference.

private void ProcessHit(DamageDealer damageDealer)
{
	health -= damageDealer.GetDamage();
	damageDealer.Hit();

	if (health <= 0)
	{
		Die();
	}
}

The ProcessHit method above reduces the health of the enemy by the value returned by the GetDamage method for the instance of the DamageDealer class that has been passed as a parameter for this method.

The next line calls the Hit method on that instance of the DamageDealer, which remember, is a component on the Player’s laser. The Hit method then calls Destroy(gameObject) which destroy’s the player’s laser.

So, there are two areas here where this could be going wrong for you;

  • there isn’t a DamageDealer.cs script component on the player’s laser prefab
  • you aren’t calling Hit as shown on the second line in the ProcessHit method

You should be able to check the prefab fairly easily. With regards to the code, to determine what is getting called when, use some Debug.Log statements and see what happens. For example, within the Enemy.cs script you could do this;

private void OnTriggerEnter2D(Collider2D collision)
{
    DamageDealer damageDealer = collision.gameObject.GetComponent<DamageDealer>();

    if (!damageDealer)
    {
        return;
    }

    Debug.Log("Found DamageDealer component on GameObject : " + collision.gameObject);
    ProcessHit(damageDealer);
}

Hope this helps :slight_smile:


Updated Wed Dec 12 2018 16:25

The above is covered in lecture Layer Collsion Matrix (#106 at the time of writing), here’s a link to the GitHub commit at that point in the course;

1 Like

yup, it was that I didn’t add damageDealer.Hit(); to ProcessHit method, thank you sir now it’s working fine :smiley:

1 Like

Great, I’m glad you can move forward with the course again and you’re more than welcome :slight_smile:

1 Like

just to point out, in the lecture Rick doesn’t mention this so I’m guessing i still haven’t got to where he tells us to write it :smiley:

Well, you know from Number Wizard that Rick has magical power, so perhaps he used slight of hand :wink:

Just ran the video at x2, run it from 05:39 and you’ll see the part where it’s mentioned… alas no slight of hand from magical Rick

image

3 Likes

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms