Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Matt5667tt · Mar 20, 2020 at 11:31 PM · 2d-platformercoin

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Matt5667tt · Mar 21, 2020 at 06:43 PM 0
Share

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);
     }
 }

}

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image streeetwalker · Mar 24, 2020 at 05:08 PM 0
Share

$$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!

avatar image Matt5667tt streeetwalker · Mar 26, 2020 at 12:53 AM 0
Share

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.

avatar image streeetwalker Matt5667tt · Mar 26, 2020 at 06:25 AM 0
Share

@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!

Show more comments
avatar image
0

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?

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Matt5667tt · Mar 22, 2020 at 04:34 PM 0
Share

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?

avatar image streeetwalker Matt5667tt · Mar 22, 2020 at 04:41 PM 0
Share

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.

avatar image streeetwalker Matt5667tt · Mar 22, 2020 at 04:50 PM 0
Share

@$$anonymous$$att5667tt, which counter are you talking about - coinCount or life Count? which one is not working?

avatar image Matt5667tt · Mar 22, 2020 at 04:36 PM 0
Share

The variables are apart of the Global Health system.

avatar image streeetwalker Matt5667tt · Mar 22, 2020 at 04:43 PM 0
Share

OK, you're talking about coinCount, not lifeCount?

avatar image streeetwalker Matt5667tt · Mar 22, 2020 at 04:44 PM 0
Share

when you say count = 100 from the beginning, which count are you referring to - lifeCount or coinCount?

avatar image Matt5667tt streeetwalker · Mar 22, 2020 at 11:56 PM 0
Share

Sorry, I'm talking about health life Count. You know, because the coin is suppose to subtract and add from player health.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

210 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to create a controller for a 2D playable character, but i get compilation error 2 Answers

Problem with Jump Animation 0 Answers

Need help with this character controller(jump doesn't work),I'm having trouble with a character controller 0 Answers

Can I make my sprites pixelated inside Unity? 0 Answers

my 2d character shakes when jumping to the side 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges