Click here to Skip to main content
15,878,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
First and foremost, these are my codes
C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Bullet : MonoBehaviour
{
    public GameObject enemyTriangle;

    void OnCollisionEnter2D (Collision2D collision)
    {
        if (enemyTriangle.tag == "normalEnemyTag")
        {
            ScoreScript.totalScore = (ScoreScript.currentScore + ScoreScript.normalEnemy);
            
        }

        if (enemyTriangle.tag == "eliteEnemyTag")
        {
            ScoreScript.totalScore = (ScoreScript.currentScore + ScoreScript.eliteEnemy);
            
        }

        if (enemyTriangle.tag == "bossEnemyTag")
        {
            ScoreScript.totalScore = (ScoreScript.currentScore + ScoreScript.bossEnemy);
        }

        Destroy(collision.collider.gameObject);
        Destroy(gameObject);
        
    }
    
}


Here is the scorescript script it was referencing to
C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ScoreScript : MonoBehaviour
{
    public static int currentScore = 0;
    public static int totalScore = 0;

    public static int normalEnemy = 100;
    public static int eliteEnemy = 200;
    public static int bossEnemy = 500;

    private static ScoreScript ScoreScriptInstance;
    public static ScoreScript instance 
    {
        get 
        {
            if (ScoreScriptInstance == null)
            {
                ScoreScriptInstance = FindObjectOfType(typeof(ScoreScript)) as ScoreScript;
            }
            return ScoreScriptInstance;
        }
    }

    public Text playerScore;

    void Update ()
    {
        playerScore.text = totalScore.ToString();
    }


}


Now, the problem. Whenever a game object was destroyed (enemy), it wasnt registering on the totalScore variable, it doesnt even give me an output on the console when I try the Debug.Log(""). It seems like it skips through the "if" statements.

What I have tried:

Merging both scripts into one but to no avail. I feel like I need to remove the other if statements but I cant think of a solution to keep all 3 enemy types.
Posted
Updated 11-May-22 7:59am
v2

1 solution

Perhaps what you want is more along the lines of:

ScoreScript.currentScore = 0;

if (enemyTriangle.tag == "normalEnemyTag") {
   ScoreScript.currentScore = ScoreScript.normalEnemy;
            
} else if (enemyTriangle.tag == "eliteEnemyTag") {
   ScoreScript.currentScore = ScoreScript.eliteEnemy;
            
} else if (enemyTriangle.tag == "bossEnemyTag") {
   ScoreScript.currentScore = ScoreScript.bossEnemy;
}

ScoreScript.totalScore += ScoreScript.currentScore;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900