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?
Here's a the global health script if you need it
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Scene$$anonymous$$anagement;
public class PlayerHealth : $$anonymous$$onoBehaviour {
public static int currentHealth = 100;
public int internalHealth;
void Update()
{
internalHealth = currentHealth;
if (currentHealth <= 0)
{
Scene$$anonymous$$anager.LoadScene(1);
}
}
}
Answer by Matt5667tt · Mar 24, 2020 at 12:42 AM
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.
$$anonymous$$att! yes, details! You need to connect the scripts together. lifeCount us created as a public variable in your PlayerHealth class, but it is not the same lifeCount that you've declared in your CoinCounter class.
Any variable you declare as static is available from any other script just by prefacing the variable name with the class name - I thought you knew that because you are setting PlayerHealth.currentHealth where you count the coins.
So, because you declared lifeCount as static in CoinCount, get rid of the 'public int lifeCount' declaration in PlayerHealth, and change your line of code to:
CoinCount.lifeCount = currentHealth;
OR, after you are done counting up all your coins:
CoinCount.lifeCount = PlayerHealth.currentHealth;
OR, , where you display lifeCount, add this line of code:
lifeCount = PlayerHealth.currentHealth;
Do any one of those and it should solve your problem!
Good news, the coins are subtracting properly. Bad news, the counter is still starting at zero. But when the coins subtract, it doesn't go negative, it was still positive. $$anonymous$$eaning it was keeping the numbers value. I thought it was because of the size of the UI, but nope, it still acts the same. Can you help me again. The line of code that worked for me was this:
coinDisplayBox.SetActive(true);
PlayerHealth.currentHealth -= 1;
theCoin.SetActive(false);
CoinCounter.lifeCount = PlayerHealth.currentHealth;
And I the scripts won't work unless I have that lifeCount variable in PlayerHealth, thankyou for your help.
@matt566tt , When you say the scripts won't work unless you have lifeCount in player health, exactly what do you mean by "won't work" ? If you are getting errors because you removed
public int lifeCount;
from your PlayerHealth class, then you have something else going on. Do you have other scripts using these varaibles
I would suggest simplifying eveything, create a new static class for all the player stats, do not attach it to any gameObject:
public static class PlayerStats {
public static int coinCount;
public static int lifeCount;
public static int currentHealth;
}
Then create a game/player controller class that combines the functions for your PlayerHealth class and CoinCounter class, and place it on an empty gameObject in your scene. After you do this, remove the PlayerHealth script component, and CoinCounter script components from your objects - you don't need them any more.
public class GameController : $$anonymous$$onoBehaviour {
// set all these public variable's values in the inspector:
public GameObject coinageDisplay;
public GameObject lifeDisplay;
public int manager_currentHealth = 100;
// do not set these, they will display values as the game runs
public int internalCoinage;
public int internalLife;
void Start() {
PlayerStats.currentHealth = manager_currentHealth;
PlayerStats.coinCount = 0;
PlayerStats.lifeCount = PlayerStats.currentHealth;
}
void Update() {
PlayerStats.lifeCount = PlayerStats.currentHealth;
internalCoinage = PlayerStats.coinCount;
coinageDisplay.GetComponent<Text>().text = "" + PlayerStats.coinCount;
internalLife = PlayerStats.lifeCount;
lifeDisplay.GetComponent<Text>().text = "" +PlayerStats.lifeCount;
if ( PlayerStats.currentHealth <= 0 ) {
Scene$$anonymous$$anager.LoadScene(1);
}
}
Then everywhere you reference cointCount, currentHealth and lifeCount in any other script, change the references to start with PlayerStats - so ins$$anonymous$$d of CoinCounter.coinCount = use PlayerStats.coinCount = , and like wise PlayerStats.lifeCount, and PlayerStats.currentHealth
If you do all this, it will put everything in one place where it is easier to debug if you have some kind of problem!
Answer by streeetwalker · Mar 22, 2020 at 11:07 AM
@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?
I want to make two types of coins, one where you can subtract health and one where it can add health. Like the 1up mushroom and the poison mushroom in Super $$anonymous$$ario. When I start the game, my counter instantly starts at 0, even though it was set to 100. And when I collect a coin, it doesn’t add nor subtract from the counter. Even though the regular coins are perfectly fine. It’s apart of a separate point system for coins. I tried many things and searched the internet for any answers, finding nothing. What do I do?
OK, that helps, @$$anonymous$$att5667tt It was clear in your code that you want 2 types of coins, and that you are subtracting and adding to player health. But you are not making the connection from player health to life.
No where to you set life to something other than 0 to begin with, and you don't ever set life = player health. LifeCount, internalLife are just sitting there on their own doing nothing. So internalLife and lifeCount never change.
@$$anonymous$$att5667tt, which counter are you talking about - coinCount or life Count? which one is not working?
OK, you're talking about coinCount, not lifeCount?
when you say count = 100 from the beginning, which count are you referring to - lifeCount or coinCount?
Sorry, I'm talking about health life Count. You know, because the coin is suppose to subtract and add from player health.