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 ();	
    	}
    
    }
    	
    }

scoreText.text gives string not int :slight_smile:

change it to

 scoreText.text = score.ToString ();  

or

   scoreText.text = ""+ score;

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

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.”