- Home /
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?
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;
}
}
Your answer
Follow this Question
Related Questions
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
Score Multiplier by Time 1 Answer
I want my score to reset back to 0 but keep my highscore saved 3 Answers
How do I do I time-base score script 0 Answers