• 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
Question by CodeGeas · Nov 26, 2017 at 08:32 AM · collectiblecollectible-game-objects

How to add a coin sytem to my game?

Hello everyone, I'm trying to make a coin system to my game but I'm facing these two problems: 1) The text doesn't display anyt$$anonymous$$ng in the game screen, but i can see it when i'm in build mode 2) It doesn't show anyt$$anonymous$$ng until i pickup a coin, and when i do, it only says 1 no matter how many coins i pick up. My coins are all tagged with cointag I've double checked, and they do get destroyed, but it doesn't make the int coin go up. I also added using UnityEngine.UI. I'd appreciate if someone could answer my question. Thanks

public class SnowmanController : MonoBehaviour { // Update is called once per frame public int coin; public Text coinText;

 void Update ()
 {
     coin = 0;

     if (Input.GetKey (KeyCode.RightArrow)) {
         transform.position += new Vector3 (0.1f, 0, 0);
     } else if (Input.GetKey (KeyCode.LeftArrow)) {
         transform.position -= new Vector3 (0.1f, 0, 0);
     } if (Input.GetKey (KeyCode.UpArrow) | Input.GetKey (KeyCode.Space)) {
         transform.position += new Vector3 (0, 0.1f, 0);
     } else if (Input.GetKey (KeyCode.DownArrow)) {
         transform.position -= new Vector3 (0, 0.1f, 0);
     } else if (Input.GetKey (KeyCode.R)) {
         SceneManager.LoadScene ("Level1");
     }


 }
 void OnCollisionEnter2D(Collision2D other){
     if (other.gameObject.tag == "cointag") {
         Destroy (other.gameObject);
         coin = coin + 1;
         coinText.text = "Coin: " + coin.ToString ();
     } else if (other.gameObject.tag == "treetag") {
         SceneManager.LoadScene ("Level 2");
     }
 }

}

Comment

People who like this

0 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 GothSeiDank · Nov 26, 2017 at 09:34 AM 1
Share

You are setting coin to 0 in every frame in the Update function.

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by pluetoe · Dec 02, 2017 at 04:29 AM

Move the "Coin" variable out of the "update function", because it resets the coin value to one every frame.

e.g

// initialize the value to zero here

coin = 0;

void Update () {}...

T$$anonymous$$s should fix your problem :)

Comment
Buckslice

People who like this

1 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

73 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

Related Questions

I have multiple collectibles that can be pressed to add to score. But how do I have it so a collectible can only count once to the score? 1 Answer

How do I keep objects destroyed between scenes? 1 Answer

collectable currency doesn't transfer between different scenes 0 Answers

I need help making a score system for a First Person Controller 1 Answer

How do I make an object appear on screen after it is collected? 1 Answer


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