Hi, I’ve gotten to the end of this section so I started adding to the game to add new features etc, however, I noticed a bug that I’m not sure how to fix;
private void OnTriggerEnter2D(Collider2D collision) {
Projectiles missile = collision.gameObject.GetComponent();
if (missile) {
enemyHealth -= missile.GetDamage();
if (enemyHealth <= 0) {
int DropRate = Random.Range(0, 10);
print(DropRate);
EnemyDeath();
}
}
}
I’m pretty sure what’s happening is when 2 or more bullets hit the enemy while it’s at 0 health or under 0 health, it causes it’s death method to happen twice, so when I have the bullets shooting every 0.1 seconds, many can hit an enemy ship as it dies, and it causes the points to go up twice, so in my case each ship gives 200 points, and when the bug happens it goes up by 400, I hope you understand what I mean and thanks for any help.
There are a couple of ways to fix it, you could for example delete the collider after it dies, but I don’t think it is optimal. The approach I would take would be to create a bool hasDied = false; and then you could check if it has died or not before you handle the damage and death method, something like this:
It is happening because in the start method you are not passing the value to the playerController variable, you are trying to access an empty variable, it should be:
This should work if the PlayerController script is within the same GameObject, otherwise if it is not you would have to use playerController =FindObjectOfType<PlayerController>(); instead.
Perfect, I had your first suggestion already but as you said if the script isn’t already on the object it won’t find it so I used playerController =FindObjectOfType<PlayerController>(); and it worked, thanks again!
Although the code that you have pasted here you were using a playerController.GetComponent and not playerController = GetComponent, if it was indeed what you used and the script is in fact in the same GameObject, then this was the source of the problem