Subtracting Score on Collide subtracts one after first collision, but next time it subtracts two

Hello,
Yes, trivial issue for some, but for some reason I am not seeing how its subtracting two from the score after second collision. I have a method void OnTriggerEnter(Collider other)

I have an if statement based on a tag

//When player collides we need to take the actions below
        if (other.CompareTag ("Player"))
        {
            //the explotion animation and audio will occur
            Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
            if(gamecontroller.life > 0)
            {
                //Subtract life from player
               [B] [/B]gamecontroller.life--;
                //Notify user of player destruction, Instiate new player after timer
                gamecontroller.DeadPlayer();
            }         
            else
            {
                //No Life left game over
                gamecontroller.GameOver();
            }
        }

I have a GameController script and as you can see I subtract each time and then I call a method called DeadPlayer()

 //When player is destroyed change value of variable to true
    public void DeadPlayer()
    {
        StartCoroutine(DeadPlayerPause());
    }

I am sure there is a better way, but this works, than I StartCoroutine so I can pause before new object is Instantiated.

    //DeadPlayer trying to pause before starting again if player still has lifes.
    IEnumerator DeadPlayerPause()
    {
        deadPlayer = true;
        gameOverText.text = "Ship Destroyed, don't give up, Keep fighting";
        yield return new WaitForSeconds(7);
        gameOverText.text = "";
        Instantiate(Player);
        StartCoroutine(SpawnWaves());
    }
    }

I can’t figure out where the double count is coming from, anyone have any ideas? If I am missing some code that may help, let me know.

Spawn wave that makes the items drop randomly.

    //This is how we spawn the enemy objects and randomly have the falling on screen.
       IEnumerator SpawnWaves()
    {
        yield return new WaitForSeconds(startWait);
        while (true) {
             for (int i = 0; i < hazardCount; i++) {
                GameObject hazard = hazards[Random.Range(0, hazards.Length)];
                Vector3 spawnPosition = new Vector3(Random.Range(-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
                Quaternion spawnRotation = Quaternion.identity;
                Instantiate(hazard, spawnPosition, spawnRotation);
                yield return new WaitForSeconds(spawnWait);
            }
            yield return new WaitForSeconds(waveWait);

            //This will break the while loop and prevent more enemies from being spawned.
            if (gameOver)
            {
                restartText.text = "Press 'R' for Restart or 'Q' to Quit";
                restart = true;
                break;

            }
            //This will break the while loop and prevent more enemies from being spawned.
            if (deadPlayer)
            {
                deadPlayer = false;
                break;
            }

        }
    }

@millerizi Have you tried Debugging the script , just add Debug.Log(“Life Subtracted”); after line 9 in your first code , and see how many times is the code being called , sometimes the collisions are detected more than one time, so do debug your code and let me know the results.

1 Like

You’ll most likely find it’s two collisions over two frames, but there is nothing in the collision logic which checks at all and from what I can see you’re not destroying gameobjects or disabling any colliders upon “death”. Simplest tweak would be something like, in the first script, line 6, add " && !deadPlayer)" or something similar,

1 Like

@Magnas94 Thanks for the advice, I am going to start doing that on everything, you probably can tell I am a little new to this.I did the debug and its only showing once each time object is killed, I am going to use Debug.Log and kind of walk through all of it, I am guessing I have something out of wack on the Destroy by Contact CS file.

@LeftyRighty I destroy the object after the if statement is completed, I am not sure about the && !deadPlayer. Are you saying and deadPlayer is NOT EQUAL to true?

Thanks for taking the time to look at post.

I think I fixed it, I got rid of the sub trigger and add the life validation to each section, this seems to fix the problem, using the Debug.Log and the information you gave me Lefty REALLY Helped. Thank you. Below is what I did to take a look and fix.

        //When player collides we need to take the actions below
        if (other.CompareTag ("Player") && gamecontroller.life > 0)
        {
            Debug.Log("CompareTag (Player)");
           
            //the explosion animation and audio will occur
            Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
 
            //Subtract life from player
            gamecontroller.life--;

            Debug.Log("Life Subtracted");
           
            //Notify user of player destruction, Instiate new player after timer
            gamecontroller.DeadPlayer();
               
        }
        if (other.CompareTag("Player") && gamecontroller.life <= 0)
        {
            //No Life left game over
            Debug.Log("Game OVER");
            gamecontroller.GameOver();
               
         }