How to subtract health from a coin(2D)

I have a coin system where when you get a coin, you get a point. There are multiple coins in the system. Apart of that is a coin that subtracts life and one that gives. I’m using the same coin system for the original. There are two scripts as seen here.

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

public class CoinCounter : MonoBehaviour
{
public static int coinCount;
public static int lifeCount;
public GameObject coinageDisplay;
public GameObject lifeDisplay;
public int internalCoinage;
public int internalLife;

void Update ()
{
    internalCoinage = coinCount;
    coinageDisplay.GetComponent<Text>().text = "" + coinCount;

    internalLife = lifeCount;
    lifeDisplay.GetComponent<Text>().text = "" + lifeCount;
}

}


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

public class Coin : MonoBehaviour
{
public enum Amount { one, five, ten, fifty, N_one, N_five, N_ten, N_fifty, G_one };
public Amount type;
public GameObject theCoin;
public GameObject coinDisplayBox;

void OnTriggerEnter2D(Collider2D other)
{
        if (type == Amount.one)
        {
            coinDisplayBox.SetActive(true);
            CoinCounter.coinCount += 1;
            theCoin.SetActive(false);
        }else if (type == Amount.five)
        {
            coinDisplayBox.SetActive(true);
            CoinCounter.coinCount += 5;
            theCoin.SetActive(false);
        }else if (type == Amount.ten)
        {
            coinDisplayBox.SetActive(true);
            CoinCounter.coinCount += 10;
            theCoin.SetActive(false);
        }else if (type == Amount.fifty)
        {
            coinDisplayBox.SetActive(true);
            CoinCounter.coinCount += 50;
            theCoin.SetActive(false);
        }else if (type == Amount.N_one)
        {
            coinDisplayBox.SetActive(true);
            PlayerHealth.currentHealth -= 1;
            theCoin.SetActive(false);
        }else if (type == Amount.N_five)
        {
            coinDisplayBox.SetActive(true);
            PlayerHealth.currentHealth -= 5;
            theCoin.SetActive(false);
        }else if (type == Amount.N_ten)
        {
            coinDisplayBox.SetActive(true);
            PlayerHealth.currentHealth -= 10;
            theCoin.SetActive(false);
        }else if (type == Amount.N_fifty)
        {
            coinDisplayBox.SetActive(true);
            PlayerHealth.currentHealth -= 50;
            theCoin.SetActive(false);
        }else if (type == Amount.G_one)
        {
            coinDisplayBox.SetActive(true);
            PlayerHealth.currentHealth += 1;
            theCoin.SetActive(false);
        }
    }
}

The real problem is with this line

    internalLife = lifeCount;
    lifeDisplay.GetComponent<Text>().text = "" + lifeCount;

And this is also going to take in the fact that there is a subtraction coin too. Is there something I can do, or do I need a seprate script?

@Matt5667tt, you haven’t really described the problem.

You have some code that could be made a lot simpler.

And where is the connection between player.currentHealth and lifeCount, or internalHealth (why do you have both of those?).

You have these variables:

  • internalLife
  • lifeCount
  • internalHealth
  • currentHealth

What is supposed to be the relationship between them?

I can see how you might be confused, but what is the problem?

Thankyou streetwalker for the help, but I think I forgot to clarify some things.

I have three different types of scripts, Coin, CoinCounter, and PlayerHealth.

Coin:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Coin : MonoBehaviour
{
public enum Amount { one, five, ten, fifty, N_one, N_five, N_ten, N_fifty, G_one };
public Amount type;
public GameObject theCoin;
public GameObject coinDisplayBox;

void OnTriggerEnter2D(Collider2D other)
{
        if (type == Amount.one)
        {
            coinDisplayBox.SetActive(true);
            CoinCounter.coinCount += 1;
            theCoin.SetActive(false);
        }else if (type == Amount.five)
        {
            coinDisplayBox.SetActive(true);
            CoinCounter.coinCount += 5;
            theCoin.SetActive(false);
        }else if (type == Amount.ten)
        {
            coinDisplayBox.SetActive(true);
            CoinCounter.coinCount += 10;
            theCoin.SetActive(false);
        }else if (type == Amount.fifty)
        {
            coinDisplayBox.SetActive(true);
            CoinCounter.coinCount += 50;
            theCoin.SetActive(false);
        }else if (type == Amount.N_one)
        {
            coinDisplayBox.SetActive(true);
            PlayerHealth.currentHealth -= 1;
            theCoin.SetActive(false);
        }else if (type == Amount.N_five)
        {
            coinDisplayBox.SetActive(true);
            PlayerHealth.currentHealth -= 5;
            theCoin.SetActive(false);
        }else if (type == Amount.N_ten)
        {
            coinDisplayBox.SetActive(true);
            PlayerHealth.currentHealth -= 10;
            theCoin.SetActive(false);
        }else if (type == Amount.N_fifty)
        {
            coinDisplayBox.SetActive(true);
            PlayerHealth.currentHealth -= 50;
            theCoin.SetActive(false);
        }else if (type == Amount.G_one)
        {
            coinDisplayBox.SetActive(true);
            PlayerHealth.currentHealth += 1;
            theCoin.SetActive(false);
        }
    }
}

CoinCounter:

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

public class CoinCounter : MonoBehaviour
{
public static int coinCount;
public static int lifeCount;
public GameObject coinageDisplay;
public GameObject lifeDisplay;
public int internalCoinage;
public int internalLife;

void Update ()
{
    internalCoinage = coinCount;
    coinageDisplay.GetComponent<Text>().text = "" + coinCount;

    internalLife = lifeCount;
    lifeDisplay.GetComponent<Text>().text = "" + lifeCount;
}

}

PlayerHealth:

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

public class PlayerHealth : MonoBehaviour
{

public static int currentHealth = 100;
public int lifeCount;  ////set to zero from the start

void Update()
{
    lifeCount = currentHealth;
    if (currentHealth <= 0)
    {
        SceneManager.LoadScene(1);
    }
}

}

I am having trouble trying to connect and/or the code itself. All of the problems you described where already stated. Thankyou for clarifying it for me. I would suggest checking the CoinCounter script, that is where I think the problem is.