- Home /
 
Space Shooter DestroyByContact Script outputs double the amount of score inputted for asteroid
I'm now on the "Ending the Game" part of the Space Shooter tutorial and now I've got a minor yet strange issue. For some reason, when I assign the Asteroid prefab a value of 10 for score, it adds 20 score points when one asteroid is destroyed. Also, it adds 10 points whenever the player explodes from colliding with an asteroid. I've tested to see if it'll add 10 points if I assign the prefab a value of 5, and it does. Interestingly, I've checked the hierarchy and it shows TWO explosions for whenever one asteroid is destroyed. Can anybody help me with this? Here's my DestroyByContact script if that helps.
 using UnityEngine;
 using System.Collections;
 
 public class DestroyByContact : MonoBehaviour
 {
     public GameObject explosion;
     public GameObject playerExplosion;
     public int scoreValue;
     private GameController gameController;
 
     void Start()
     {
         GameObject gameControllerObject = GameObject.FindWithTag("GameController");
         if (gameControllerObject != null)
         {
             gameController = gameControllerObject.GetComponent <GameController>();
         }
         if (gameController == null)
         {
             Debug.Log("Cannot find 'GameController' script");
         }
     }
 
 
 
     void OnTriggerEnter(Collider other)
     {
         if (other.tag == "Boundary")
         {
             return;
         }
         Instantiate(explosion, transform.position, transform.rotation);
         if (other.tag == "Player")
         {
             Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
             gameController.GameOver();
         }
         gameController.AddScore(scoreValue);
         Destroy(other.gameObject);
         Destroy(gameObject);
         
     }
 }
 
               Here's a screenshot of the player getting 20 points when destroying one asteroid. Note how there's two asteroid explosions in the hierarchy. 
On which gameObject is this script attached to?. $$anonymous$$aybe you have 2 different objects on which this same script is attached?
@aditya007 Nope, it's Just on the Asteroid prefab.
And the bullet prefab does not have this script attached to it?
The way I see it, it has to mean you are getting the trigger twice. The first thing I would look at is that you are adding the score outside of the if statements so it means any call to the trigger is going to call addScore unless it's a boundary trigger. Could it be the explosion is triggering a second event? $$anonymous$$aybe the explosion is triggering another call to the trigger?
I would put Print("trigger called"); inside the trigger function and Print("collided with player") inside the if other.tag == player. wouldn't hurt to put another one for inside the boundary.
Then watch your console and see how many calls you're getting according to the activity in the game.
@Animus428 Nope, I'm not getting any calls. Am I doing this correctly, just to make sure?
@Drew7398 can we have your GameController class, as when i checked the original class from tutorial itself i can't find the GameOver method in it and this makes me to think that probably you are adding 10 in score in this game over method too
@aditya Sure, here it is.
 using UnityEngine;
 using System.Collections;
 
 public class GameController : $$anonymous$$onoBehaviour
 {
     public GameObject hazard;
     public Vector3 spawnValues;
     public int hazardCount;
     public float spawnWait;
     public float startWait;
     public float waveWait;
  
     public GUIText scoreText;
     public GUIText restartText;
     public GUIText gameOverText;
 
     private bool gameOver;
     private bool restart;
     private int score;
 
     
     
 
     void Start ()
     {
         gameOver = false;
         restart = false;
         restartText.text = "";
         gameOverText.text = "";
         score = 0;
         UpdateScore();
         StartCoroutine (SpawnWaves());
         
     }  
     
     void Update ()
     {
         if (restart)
         {
             if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.R))
             {
                 Application.LoadLevel(Application.loadedLevel);
             }
         }
     } 
 
     IEnumerator SpawnWaves ()
     {
         yield return new WaitForSeconds(startWait);
         while (true)
         {
             for (int i = 0; i < hazardCount; i++)
             {
                 Vector3 spawnPosition = new Vector3(UnityEngine.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);
 
             if (gameOver)
             {
                 restartText.text = "Press 'R' for Restart";
                 restart = true;
                 break;
             }
         }    
     }
 
     public void AddScore(int newScoreValue)
     {
         score += newScoreValue;
         UpdateScore();
     }
 
     void UpdateScore ()
     {
         scoreText.text = "Score: " + score;
     }
 
     public void GameOver()
     {  
         gameOverText.text = "Game Over";
         gameOver = true;
     }
 }
 
 
                  Your answer
 
             Follow this Question
Related Questions
Camera moving itself, still following an object 1 Answer
Space Shooter Sound is not working on Android device Speakers 0 Answers
How to make asteroids bounce off eachother 0 Answers
[3D] Having an object follow mouse (Star Fox like control) 0 Answers
My space ship is sliding to one side and getting stuck there, what would cause this? 1 Answer