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 TurbidWarrior · Mar 17, 2015 at 09:17 PM · string

noob coding mistake, please help

Hey, so i want to have a score system in my game, and the score goes down as you play so the faster you finish the higher your score will be. i have got the following code which i applied to a Guitext. the code starts off at 5000 and counts down in 1's every tick but i cant find a way of having the GUI text show the number. here are the relevant images and the error i get.

the empty i made GUI Text on (http://i.imgur.com/yvCc3pi.png)

The Code (i'm using C#) using UnityEngine; using System.Collections;

 public class Score : MonoBehaviour {
     public float score;
     public GUIText yourText;
     // Use this for initialization
     void Start () {
         score = 5000;
     }
     
     // Update is called once per frame
     void Update () {
         score = score - 1;
         yourText.text = score;
     }
 }

and i get this error when trying to build it, "cannot implicitly convert type float to string" and this is on the line "yourText.text = score;" thank you for your help.

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

4 Replies

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

Answer by tanoshimi · Mar 17, 2015 at 09:47 PM

You can't implicitly convert a float to a string. So do it explicitly:

  yourText.text = score.ToString();
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 TurbidWarrior · Mar 17, 2015 at 10:01 PM 0
Share

Thank you so much, as title stated it was a noob mistake :) i was trying things like .string thank you so much for your help works as intended now :)

avatar image
0

Answer by mpennin3 · Mar 17, 2015 at 10:07 PM

Why dont you just look at Beginner Roll-a-Ball at http://unity3d.com/learn/tutorials/projects/roll-a-ball/Displaying-text and fast forward to where he shows the code for it because the problem is the text.

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
avatar image
0

Answer by Dxter · Mar 17, 2015 at 10:08 PM

yourText.text = score.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
avatar image
0

Answer by dr3th · Mar 17, 2015 at 09:48 PM

Hi TurbidWarrior,

Lets break this down.

1 "cannot implicitly convert type float to string" Score is of type float. youText.Text property is of Type string.

To fix this first error:

      void Update () {
          score = score - 1;
          yourText.text = score.ToString();
      }

2 Do not use GUIText(It's no longer supported) but instead use UnityEngine.UI.Text. Please add a Text Component to your game.

 void Update()
 {
     
             score--;    
         
             GameObject DrethScoreGameObject = GameObject.Find("yourText");
                 
             Text yourText= DrethScoreGameObject.GetComponent<Text>();
                
             yourText.Text = score.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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do i take a string from a gui and show it in the gameworld c# 1 Answer

How to Instantiate from correct Icon. 1 Answer

[SOLVED] First array slot blocking second array slot 1 Answer

String cases in C# 3 Answers

C# ArrayList match to string? 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