• 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 DragonFang17 · Feb 07, 2017 at 05:38 PM · uitextcoincollectible

Coin Collecting: Updating UI Text

I may not be the only one having trouble with t$$anonymous$$s but here goes:

I'm working on currency for my game starting with getting a working UI text element for the coins the player can collect. I need each coin laying on the ground to give a random number between 100 and 500 and add it to the current amount. I have my coin game object in my scene, the UI Text on the corner of my screen, all I need is to link the two. My errors are telling me int cannot convert to GUIText nor can I convert a type float to string. Hoping I can get some help with t$$anonymous$$s, here's my code:

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

public class CoinCollect : MonoBehaviour {

 public float minCoin = 100;
 public float maxCoin = 500;
 float coinAmount;
 public GUIText totalCoins;
 public int currentCurrency = 0;

 void Start() {

     coinAmount = Random.Range (minCoin, maxCoin);
     totalCoins = currentCurrency;
 }

 void Update () {
     transform.Rotate (new Vector3(0,Time.deltaTime*50,0));
 }

 void OnTriggerEnter(Collider other) {
     if (other.gameObject.tag == "Player") {
         AudioSource source = GetComponent<AudioSource> ();
         source.Play ();
         UpdateScore ();
     }
         }

 void UpdateScore () {
     totalCoins.text = currentCurrency + coinAmount;
 }

}

Comment
Add comment
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

2 Replies

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

Answer by jmgek · Feb 07, 2017 at 09:19 PM

First you need to cast it as a string, then I would change the way you're handling these, seeing how you're mixing up t$$anonymous$$ngs that should be static and not using proper patterns even though it does look like you're trying to. You want your coins to handle their own logic, you don't want your Coin Collect to handle t$$anonymous$$ngs like rotation, so:

 using UnityEngine; 
 using System.Collections; 
 using UnityEngine.UI;
 
 public class Coin : MonoBehaviour
 {
   public int coinAmount;
 
   void Update() {
     transform.Rotate(new Vector3(0, Time.deltaTime * 50, 0));
   }
 
   void OnTriggerEnter(Collider other) {
     //Don't use tags, they're slower
     if (other.gameObject.name == "Player") {
       AudioSource source = GetComponent<AudioSource>();
       source.Play();
       UpdateScore();
     }
   }
 
   static void UpdateScore() {
   //You want t$$anonymous$$s to be static because it's a function shared among all the Coins so only one instance of it. 
     CoinCollect.totalCoins.text = (string)(CoinCollect.currentCurrency += t$$anonymous$$s.coinAmount);
   }
 };
 
 public class CoinCollect : MonoBehaviour {
 
   public int minCoin = 100, maxCoin = 500;
   public static GUIText totalCoins;
   public static int currentCurrency = 0;
   
   void Start() {
     Coin coin = new GameObject("coin").AddComponent<Coin>();
     //Put your Coin gameobject where ever you need it
     coin.coinAmount = Random.Range(minCoin, maxCoin);
     t$$anonymous$$s.totalCoins.text = (string)currentCurrency;
   }
 
Comment
Add comment · Show 6 · 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 DragonFang17 · Feb 07, 2017 at 09:50 PM 0
Share
avatar image jmgek DragonFang17 · Feb 08, 2017 at 05:07 PM 0
Share
avatar image DragonFang17 jmgek · Feb 09, 2017 at 01:21 AM 0
Share
Show more comments
avatar image DragonFang17 · Feb 09, 2017 at 01:30 AM 0
Share
avatar image BlockFade · Feb 09, 2017 at 05:52 PM 0
Share
avatar image
1

Answer by BlockFade · Feb 08, 2017 at 09:24 AM

T$$anonymous$$s should work in the UpdateScore function.

 void UpdateScore () {
      totalCoins.text = "" + currentCurrency + coinAmount;
  }
 



Comment
Add comment · Show 2 · 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 DragonFang17 · Feb 09, 2017 at 01:28 AM 0
Share
avatar image jmgek DragonFang17 · Feb 09, 2017 at 01:34 AM 1
Share

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

6 People are following this question.

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

Related Questions

Graphic problem with UI Text 2 Answers

New UI: change text 2 Answers

UGUI Find tapped character in textfield? 1 Answer

How to attach a canvas to a game object properly? 1 Answer

Item name UI/GUI text over item/enemy in 3d 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