Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Drew7398 · Nov 09, 2016 at 08:30 AM · space shooter

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. alt text

unity-space-shooter-asteroid.png (470.2 kB)
Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aditya007 · Nov 09, 2016 at 10:14 AM 0
Share

On which gameObject is this script attached to?. $$anonymous$$aybe you have 2 different objects on which this same script is attached?

avatar image Drew7398 aditya007 · Nov 09, 2016 at 09:36 PM 0
Share

@aditya007 Nope, it's Just on the Asteroid prefab.

avatar image aditya007 Drew7398 · Nov 10, 2016 at 04:04 AM 0
Share

And the bullet prefab does not have this script attached to it?

Show more comments
avatar image Animus428 · Nov 10, 2016 at 03:43 PM 2
Share

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.

avatar image Drew7398 Animus428 · Nov 11, 2016 at 01:18 AM 0
Share

@Animus428 Nope, I'm not getting any calls. Am I doing this correctly, just to make sure?alt text

unity-script-space-shooter.png (67.3 kB)
avatar image aditya · Nov 11, 2016 at 05:19 AM 0
Share

@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

avatar image Drew7398 aditya · Nov 11, 2016 at 04:24 PM 0
Share

@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;
     }
 }
 
 








0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges