• 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 JL_UKnl12 · Apr 18 at 09:25 PM · uiontriggerenterscorescore systemkills

Score increases based on number of collisions at time of kill, not on single kill.,Score increases based on collisions at time of death, not based on single kill.

I'm creating a space shooter game, and for it I've set up the two scripts below, so that the Game Manager will automatically update the player score upon killing a monster. However, I've encountered the following problem where the colliders of my laser objects and the below OntriggerEnter(Collider other) void function interact:

  • I've got three prefab monsters which respectively yield a pointValue score of 100, 200, and 300.

  • The player's ship has two types of attacks: a standard twin-laser attack shaped like \ / , where each of the two laser shots that constitute the single prefab have their own capsule collider, and a powerful spinning charged attack with six branches in the shape of a large circle. Again, each of the six branches have their own capsule collider attached.

  • The OnTriggerEnter void functions calls upon DeathEnemy, which in turn calls the YieldScore function when an enemy is killed. However, as the OnTriggerEnter script ultimately calls upon the YieldScore function following every single hit against the enemy instead of only upon death, the score can increase by up to 6*300=1800 instead of a maximum of 300.

The EnemyDeath script is as follows:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class EnemyDeath : MonoBehaviour { private float destroyBoundsZ = -415;

 public int health;

 public ParticleSystem enemyExplosionParticle;
 public int pointValue;

 private GameManager gameManager;



 // Start is called before the first frame update
 void Start()
 {
     gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
 }

 // Update is called once per frame
 void Update()
 {
     ExitArena();

     
 }

 private void OnTriggerEnter(Collider other)
 {
     if (other.gameObject.CompareTag("Laser"))
     {
         
         Destroy(other.gameObject);
         DeathEnemy();
         
     }

     if (other.gameObject.CompareTag("ChargedLaser"))
     {                        
         DeathEnemy();            

     }
 }

 private void ExitArena()
 {
     if (transform.position.z < destroyBoundsZ)
     {
         Destroy(gameObject);
     }
 }

 private void DeathEnemy()
 {        
     if (health <= 0)
     {
         AudioManager.instance.EnemyDeathSound();
         Destroy(gameObject);            
         Instantiate(enemyExplosionParticle, transform.position, enemyExplosionParticle.transform.rotation);
         YieldScore();

     }

     if (health >= 0)
     {
         health -= 1; //replace later with damage variable for specific laser
     }

 }

 private void YieldScore()
 {
     if (health <= 0)
     {
         gameManager.UpdateScore(pointValue);
     }
 }

}

The GameManager script is as follows:

using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro; using UnityEngine.SceneManagement; using UnityEngine.UI;

public class GameManager : MonoBehaviour { public TextMeshProUGUI scoreNumber; private int score;

 // Start is called before the first frame update
 void Start()
 {
     
 }

 // Update is called once per frame
 void Update()
 {
     
 }

 public void UpdateScore(int scoreToAdd)
 {
     score += scoreToAdd;
     scoreNumber.text = "" + score;
 }

}

The following is the timestamp of the game I've modelled my game on. Like in the below clip, killing an enemy using the charged attack should only yield the maximum points value as specified in pointValue. https://youtu.be/MjKg4EnoGsI?t=602

Is there a way for me to re-organise and/or amend my code so that the score increases by 100/200/300 as expected?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ogulcan_topsakal · Apr 18 at 11:21 PM

  private void DeathEnemy()
  {        
      if (health <= 0)
      {
          AudioManager.instance.EnemyDeathSound();
          Destroy(gameObject);            
          Instantiate(enemyExplosionParticle, transform.position, enemyExplosionParticle.transform.rotation);
          YieldScore();
      }
      if (health >= 0)
      {
          health -= 1; //replace later with damage variable for specific laser
      }
  }


@JL_UKnl12 On this part you call Instantiate and YieldScore after you destroy script holder game object. They are never actually works since you destroy the object. Also, It may be more accurate to use > instead of >= in the if block you use below.


Here is the quick reorganization

 private void DeathEnemy()
 {
     //later change with damage instead -1
     RemoveHealth(-1);
     if (health > 0) return;
     YieldScore();
     AudioManager.instance.EnemyDeathSound();
     Instantiate(enemyExplosionParticle, transform.position, enemyExplosionParticle.transform.rotation);
     Destroy(gameObject);        
 }
 
 private static void RemoveHealth(int damage)
 {
     if (health > 0)
     {
         health -= damage;
     }
 }

Comment
Add comment · Share
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

231 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Score Multiplier by Time 1 Answer

Score counter breaking after adding points 2 Answers

I am trying to add additional score to my current timed score but cannot find how to do this 0 Answers

How to change scoreInputField to get a score from another script/Scene 0 Answers

Score and Highscore - Highscore not saving, loading, or displaying. 0 Answers


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