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

Coin Collecting: Updating UI Text

I may not be the only one having trouble with this 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 this, 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 things 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 things 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 this 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 += this.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);
     this.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

Thanks for the reply! So @jmgek if tags are slower, what would you recommend?

avatar image jmgek DragonFang17 · Feb 08, 2017 at 05:07 PM 0
Share

Either by 'gameobject.name' Like I wrote. Or since you will only have one player use your player perfab as a comparison.

 public static GameObject myPlayer; 
 if (other.gameObject== myPlayer) {

avatar image DragonFang17 jmgek · Feb 09, 2017 at 01:21 AM 0
Share

Erm, I have three selectable characters for my game at the start XD that's why I went with tags to begin with

Show more comments
avatar image DragonFang17 · Feb 09, 2017 at 01:30 AM 0
Share

You were right @jmgek, that function needed to be static :)

avatar image BlockFade · Feb 09, 2017 at 05:52 PM 0
Share

It's Easier to just use "" and add the currency to it. But this also works!

avatar image
1

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

This 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

I tried something like this for that function and it seems to work:

void UpdateScore () { Debug.Log ("Score Updated"); totalCurrency += currentCurrency; totalCoinsDisplay.text = "" + totalCurrency; }

avatar image jmgek DragonFang17 · Feb 09, 2017 at 01:34 AM 1
Share

This is a way to do it but it's not the best practice, seeing how you're concatenating to a empty string, you could run into problems, it's always better to cast to a type:

 string myString = (string)45;

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

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