Search Unity

Score counter with use of an event system. Please help

Discussion in 'Scripting' started by argens, May 3, 2019.

  1. argens

    argens

    Joined:
    Jun 21, 2018
    Posts:
    26
    Hello,

    am trying to implement a simple score counter event. Have a space shooter game and everytime a spawned enemy is killed (one bullet = ine kill) I would like to increment the score. But for some reason th score in the game is not changing/working. Maybe someone could help me with this issue?

    In the game I am spawning the enemies (from prefabs) in front of some distance of the player.

    GameController.cs
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class GameController : MonoBehaviour
    7. {
    8.     public Player3DScript player;
    9.     public GameObject enemyPrefab;
    10.  
    11.     //score
    12.     int score = 0;
    13.     public Text scoreText;
    14.  
    15. void Update()
    16.     {
    17.         if (player != null)
    18.         {
    19.             enemySpawnTimer -= Time.deltaTime;
    20.  
    21.             if (enemySpawnTimer <= 0)
    22.             {
    23.                 enemySpawnTimer = enemySpawnInterval;
    24.  
    25.                 GameObject enemyInstance = Instantiate(enemyPrefab);
    26.                 enemyInstance.transform.SetParent(transform);
    27.                 enemyInstance.transform.position = new Vector3(
    28.                     Random.Range(spawnLevelLimitLeft, spawnLevelLimitRight),
    29.                     -2.5f,
    30.                     player.transform.position.z + enemySpawnDistance
    31.                     );
    32.  
    33.                 enemyInstance.GetComponent<Enemy>().OnKill += OnEnemyKilled;
    34.                 Debug.Log("Am in UPDATE Game Controller OnEnemyKilled()");
    35.             }
    36.  
    37.             foreach (Enemy enemy in GetComponentsInChildren<Enemy>())
    38.             {
    39.                 if (player.transform.position.z >= enemy.transform.position.z + 10f)
    40.                     Destroy(enemy.gameObject);
    41.             }
    42.         }
    43.     }
    44.  
    45.     void OnEnemyKilled()
    46.     {
    47.         score += 1;
    48.         scoreText.text = "Score: " + score;
    49.         Debug.Log("Am in Game Controller OnEnemyKilled() ");
    50.     }
    51. }
    Enemy.cs
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Enemy : MonoBehaviour {
    6.  
    7.     public delegate void KilledHandler();
    8.     public event KilledHandler OnKill;
    9.  
    10. void OnTriggerEnter2D(Collider2D otherCollider)
    11.     {
    12.         if (otherCollider.tag == "PlayerBullet" || otherCollider.tag == "Player")
    13.         {
    14.             Destroy(gameObject);
    15.             Destroy(otherCollider.gameObject);
    16.  
    17.             if (OnKill != null)
    18.             {
    19.                 Debug.Log(Am in -> onKill()");
    20.                OnKill();
    21.            }
    22.        }
    23.    }
    24. }
    The text field is a simple Text object in canvas.

    When I start the game I only see the Debug.Log under:
    Debug.Log("Am in UPDATE Game Controller OnEnemyKilled()");

    Also when I kill an enemy, nothing changes. No other Debug.Log uppears.


    Any help is really welcome :).
     
  2. TaleOf4Gamers

    TaleOf4Gamers

    Joined:
    Nov 15, 2013
    Posts:
    825
    Code (CSharp):
    1. void OnTriggerEnter2D(Collider2D otherCollider)
    2.     {
    3.         if (otherCollider.tag == "PlayerBullet" || otherCollider.tag == "Player")
    4.         {
    5.             Destroy(gameObject);
    6.             Destroy(otherCollider.gameObject);
    7.             if (OnKill != null)
    8.             {
    9.                 Debug.Log(Am in -> onKill()");
    10.               OnKill();
    11.           }
    12.       }
    13.   }
    14. }
    Not sure if it matters (Perhaps?) but surely you should call the event before destroying the enemy.
     
  3. argens

    argens

    Joined:
    Jun 21, 2018
    Posts:
    26
    Have tried it but with no effect :/.
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @argens

    You could also just have a common static event for all enemies, and make other class listen to it. Note, this is not your class, just a demo.

    Code (CSharp):
    1. public class Enemy : MonoBehaviour
    2. {
    3.     public static Action<Enemy> EnemyWasDestroyed = delegate { };
    4.  
    5.     void OnTriggerEnter2D(Collider2D other)
    6.     {
    7.         EnemyWasDestroyed.Invoke(this);
    8.         Destroy(this.gameObject);
    9.     }
    10. }
    And then in your other class, that needs to know about enemies:

    Code (CSharp):
    1. void OnEnable()
    2. {
    3.     Enemy.EnemyWasDestroyed += EnemyWasDestroyed;
    4. }
    5.  
    6. void OnDisable()
    7. {
    8.     Enemy.EnemyWasDestroyed -= EnemyWasDestroyed;
    9. }
    10.  
    11. void EnemyWasDestroyed(Enemy enemy)
    12. {
    13.     Debug.Log("Enemy destroyed:" + enemy.name);
    14. }
     
    Last edited: May 3, 2019