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 /
  • Help Room /
This question was closed Apr 25, 2018 at 11:52 PM by S_jay1 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by S_jay1 · Jan 23, 2018 at 06:10 PM · errortextscore systemtype

Why is it giving me this error "cannot implicitly convert type 'int' to 'string'"

I'm trying to create a scoring system for my game, but it's giving me this error "cannot implicitly convert type 'int' to 'string'." I want to increase the score by 1 every time the player's z position increases by 10. This is my code right now:

     using System.Collections;
     using UnityEngine.UI;
     using System.Collections.Generic;
     using UnityEngine;
     
     public class Score : MonoBehaviour {
     Text scoreText;
     private int score;
     Vector3 _lastPosition;
 
 
     void UpdateScore()
 
     {
         if( ( player.transform.position.z - _lastPosition.z ) > 10 )
             score++;
 
         _lastPosition = player.transform.position;
         scoreText.text = score; //error here
 
 
     }
 
         void Start () {
     scoreText = gameObject.GetComponent<Text>();
             scoreText.text = score; // error here
             score = 0;
     }
     
     void Update()
         {
     if (player.transform.position.z > 10)
             {
         
         
            _lastPosition = player.transform.position;
             UpdateScore ();    
         }
     
     }
         
     }
     
     
     
 
 
 

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

3 Replies

  • Sort: 
avatar image
3
Best Answer

Answer by PersianKiller · Jan 23, 2018 at 06:16 PM

scoreText.text gives string not int :)

change it to

  scoreText.text = score.ToString ();  


or

    scoreText.text = ""+ score;

 
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
2

Answer by gwnguy · Jan 23, 2018 at 07:48 PM

Here is the code I used:

 public class Score : MonoBehaviour
 {
     Text scoreText;
     private int score;
     Vector3 _lastPosition;
 
     //GameObject player;
 
     void UpdateScore()
 
     {
         if ((player.transform.position.z - _lastPosition.z) > 10)
             score++;
 
         _lastPosition = player.transform.position;
         scoreText.text = score.ToString();//error here
 
 
     }
 
     void Start()
     {
         //player = GameObject.Find("Player")();
 
         scoreText = gameObject.GetComponent<Text>();
         scoreText.text = score.ToString(); // error here
         score = 0;
     }
 
     void Update()
     {
         if (player.transform.position.z > 10)
         {
 
 
             _lastPosition = player.transform.position;
             UpdateScore();
         }
 
     }
 
 }

With this, I got the following error message: Severity Code Description Project File Line Suppression State Error CS0103 The name 'player' does not exist in the current context

in four locations: lines 20,23,40,44

When I uncomment line 7

 GameObject player;


The error messages go away.

So, yes, I did assign the text object: scoreText.text = score.ToString();//error here

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 gwnguy · Jan 23, 2018 at 07:54 PM 1
Share

You might also want to confirm that the line

      scoreText = gameObject.GetComponent<Text>();

in the Start method is not null, since the next line

 scoreText.text = score; // error here

would generate a null exception if it was

  • Ahh, I see Harinezumi posted this as well.

avatar image S_jay1 gwnguy · Jan 23, 2018 at 08:42 PM 0
Share

It was the null exception, thank you for pointing out where it was!

avatar image
0

Answer by S_jay1 · Jan 23, 2018 at 07:00 PM

I tried both of them, but they both give me this error "System.NullReferenceException has been thrown: Object reference not set to an instance of an object."

Comment
Add comment · Show 4 · 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 gwnguy · Jan 23, 2018 at 07:29 PM 2
Share

The lines suggested Persion$$anonymous$$iller should work. I wonder if your error message relates to something else. I'm not sure where, in the code you've listed, you assign "player" So, it may be possible the System.NullReferenceException error relates to

 player.transform....
avatar image PersianKiller · Jan 23, 2018 at 07:37 PM 3
Share

@ S_jay1 ,did you assign text component? it seems that you didnot !! you should attach this script to the object that has text component or change

   Text scoreText;


to

  public  Text scoreText;

then drag your text component to it :)

and as @gwnguy said you didnot assigned player !!!!!

avatar image Harinezumi · Jan 23, 2018 at 07:50 PM 2
Share

Either scoreTextis null, you don't have a UI Text assigned to it, most probably because there is no Text component on your game object.

avatar image S_jay1 Harinezumi · Jan 23, 2018 at 08:35 PM 1
Share

I didn't have the UI Text assigned to it, but after I added it, I still get the error. Edit: It was the null exception that was the problem, I deleted " scoreText = gameObject.GetComponent();" and it fixed the issue. Thanks all!

Follow this Question

Answers Answers and Comments

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

Related Questions

GUI Text not showing up :( 7 Answers

WWW Text Returning Anti-Robot HTML String 0 Answers

[SOLVED] Stop counting score code error! 1 Answer

How do add scenes on the Countdown Timer 0 Answers

I have a score displayed constantly in my game, but I can't get it to work for my game-complete screen? Help? 0 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