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 /
avatar image
0
Question by Obi02_ · Aug 19, 2017 at 06:24 AM · scripting problemuicollision detectionnot workingboxcollider2d

Coin collection script (not working)

I'm creating a 2D game and made these scripts for three collectible coin types, gold, silver and copper. Each coin gameobject has a 2D box collider that is set to trigger. Each coin type is also attached to a UI text , one for coining gold coins, one for counting silver, and one for counting copper. The three UI text elements are attached to the one canvas that displays on the screen when run.
Gold coin script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class goldcoin : MonoBehaviour {

 public GameObject gcoin;
 public Text GcountText;
 private int Gcount;

 // Use this for initialization
 void Start () {

     Gcount = 0;
     SetCountText ();

 }
 
 // Update is called once per frame
 void Update () {
 }

 void OnTriggerEnter2D() 
 {
     Destroy(gcoin);
     Gcount = Gcount + 1;
     SetCountText ();
 
 }

 void SetCountText ()
 {
     GcountText.text = "" + Gcount.ToString ();
 }

}
Silver coin script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class silvercoin : MonoBehaviour {

 public GameObject scoin;
 public Text ScountText;
 private int Scount;

 // Use this for initialization
 void Start () {

     Scount = 0;
     SetCountText ();

 }

 // Update is called once per frame
 void Update () {
 }

 void OnTriggerEnter2D() 
 {
     Destroy(scoin);
     Scount = Scount + 1;
     SetCountText ();

 }

 void SetCountText ()
 {
     ScountText.text = "" + Scount.ToString ();
 }

}
Copper coin script:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;

public class coppercoin : MonoBehaviour {

 public GameObject ccoin;
 public Text CcountText;
 private int Ccount;

 // Use this for initialization
 void Start () {

     Ccount = 0;
     SetCountText ();

 }

 // Update is called once per frame
 void Update () {

 }

 void OnTriggerEnter2D() 
 {
     Destroy(ccoin);
     Ccount = Ccount + 1;
     SetCountText ();

 }

 void SetCountText ()
 {
     CcountText.text = "" + Ccount.ToString ();


 }

}

When the character walks into a gold coin, the coin disappears and the ui text displays '1', which is great. But when a silver coin or copper coin is collected, a '2' is displayed, which I don't want to happen. Then, when another coin is collected, of any type, the number either doubles or does nothing at all!
Please help!

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 Obi02_ · Aug 19, 2017 at 06:28 AM 0
Share

Update: Every time I run the game, the number added to the UI text chnges, which I don't understand, because the script is adding '1' to the value each time...

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Destolos · Aug 19, 2017 at 06:54 AM

So every coin inherits from one of these scripts, or is the variable gameobject always filled with the next coin? If you want to have the first point: The first thing, that has come to my attention is, that the counter variables are local per class, which means verey coin has its own counter. If you want to have one counter for each type you should use the static keyword.

Comment
Add comment · Show 1 · 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 Obi02_ · Aug 19, 2017 at 08:13 AM 0
Share

Thanks, I changed the int to a static int, and the gold coin now works! For some reason though, the silver and copper coins are doubling the variables. For each coin I collect, two is added to the counter.

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

149 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

Related Questions

How to check if BoxCollider2D collided with another BoxCollider2D? 2 Answers

How do I destroy a game object my character hit ONLY if a condition is active? 2 Answers

Particles only colliding with one of the player's two colliders? 0 Answers

Unity 2D Combat Text Issue With Multiple Enemies 2 Answers

Moving a ScrollRect child via script to focus on some point. 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