Score doubled bug

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.

1 Like

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:

bool hasDied = false;

....

private void OnTriggerEnter2D(Collider2D collision) 
{
    Projectiles missile = collision.gameObject.GetComponent();
    if (missile && hasDied == false) 
    {
        enemyHealth -= missile.GetDamage();
        if (enemyHealth <= 0) 
        {
            int DropRate = Random.Range(0, 10);
            print(DropRate);
            EnemyDeath();
            hasDied = true;
        }
    }
}

Let me know if this helps, I’m at your disposal

Thanks so much, such a simple fix just didn’t think of it, thanks again this has solved so many problems. :slight_smile:

Edit: I used the bool method.

2 Likes

I’m glad for being able to help, let me know if you need something else!

1 Like

I need a slight bit of help again, it says “Object reference not set to an instance of an object”:

private PlayerController playerController;

public float boostTotalDuration, boostRemainingDuration;

private bool hasPoweredUp = false;

// Use this for initialization
void Start () {
    playerController.GetComponent<PlayerController>();
}

// Update is called once per frame
void Update () {
    /*if (boostRemainingDuration <= 0)
    {
        boostRemainingDuration = 0;
    }
    else
    {
        boostRemainingDuration -= Time.deltaTime;
    }*/

    if (boostRemainingDuration <= 0 && hasPoweredUp == true)
    {
        playerController.totalCooldown = 1f;
        playerController.remainingCooldown = 0f;
        Debug.Log("Boost is now 0, deleting");
        Destroy(gameObject);
    } else
    {
        boostRemainingDuration -= Time.deltaTime;
    }
}

private void OnTriggerEnter2D(Collider2D collision)
{
    if(collision.gameObject.tag == "Player") {
        playerController.totalCooldown = 0.3f;
        playerController.remainingCooldown = 0f;
        boostRemainingDuration = boostTotalDuration;
        this.transform.position = new Vector3(-100, 0, 0);
        Debug.Log("???");
        hasPoweredUp = true;
    }
}

Does the console is saying the line that the problem was found?

Yes it’s on these lines:

playerController.totalCooldown = 1f;
playerController.remainingCooldown = 0f;

playerController.totalCooldown = 0.3f;
playerController.remainingCooldown = 0f;

If I comment them out it runs fine.

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:

void Start () 
{
    playerController = GetComponent<PlayerController>();
}

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.

Let me know if this helps

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!

1 Like

Awesome!

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

Oh yeah you’re right, didn’t notice that, thanks. :slight_smile:

1 Like