The question is answered, right answer was accepted
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 ();
}
}
}
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;
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
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.
It was the null exception, thank you for pointing out where it was!
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."
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....
@ 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 !!!!!
Either scoreText
is null, you don't have a UI Text assigned to it, most probably because there is no Text component on your game object.
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
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