Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Frix · Nov 29, 2012 at 01:28 PM · variablestringintpopup

'Cannot convert int to String'

Odd problem here.

Whenever the player kills an enemy, the player gets gold. I wanted to have the gold popup by the enemies death "+[gold]", which I've sort of done so far. I'm using a 3D text object for that, and now I'm trying to set the gold earned to the 3d text, but it's telling me I cant convert it to a string. Maybe I'm doing something wrong as I'm only just a beginner.

 function addGold(){
     plusPlayerGold = Random.Range(190, 300) * difficultyGoldMultiplier;
     playerScript.playerGold += plusPlayerGold;
     plusPlayerGold.ToString();
     goldText.guiText.text = plusPlayerGold;
     Instantiate(goldText, transform.position, transform.rotation);
     
 }

Also I'm not fully sure if goldText.guiText.text = plusPlayerGold; is the correct code, it was a half guess to be honest. That is the line that's giving me the error, too.

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
2
Best Answer

Answer by Landern · Nov 29, 2012 at 01:33 PM

When you did ToString() on the int, you didn't assign it to anything, you performed the extension method, but ToString() returns a type of string, if you are not assigning it to anything, it ends up wasted work. I mean after all, with how it was programmed, you would expect that the plusPlayerGold type after using ToString suddenly changes times from an int to a string... wait actually that makes sense, but isn't how it works.

 function addGold(){
     plusPlayerGold = Random.Range(190, 300) * difficultyGoldMultiplier;
     playerScript.playerGold += plusPlayerGold;
 //    plusPlayerGold.ToString(); // bad, not assigned
     goldText.guiText.text = plusPlayerGold.ToString();
     Instantiate(goldText, transform.position, transform.rotation);
 
 }
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 Frix · Nov 29, 2012 at 01:37 PM 0
Share

Oh wow okay, thankyou. That's fixed now, but I just realized, I was trying to access my 3D text through guiText. I don't know how to access the 3D text, is there a way to do that?

avatar image
1

Answer by Dave-Carlile · Nov 29, 2012 at 01:34 PM

ToString() is a function that returns the value as a string. Calling it like you do - plusPlayerGold.ToString() - without assigning the result to something doesn't accomplish anything. You want this:

 goldText.guiText.text = plusPlayerGold.ToString();

And remove the previous line where you call ToString().

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

Follow this Question

Answers Answers and Comments

12 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

Related Questions

string + int = variable 2 Answers

Converting a single String character to representative int. How? Javascript 2 Answers

How do i link a GUI text to a variable text which is a integer? 2 Answers

I can't print a int.ToString() variable in a guiText.text 1 Answer

Convert Array to String 3 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