Search Unity

|Error Code CS0029

Discussion in 'Scripting' started by Arthur1610, Feb 19, 2021.

  1. Arthur1610

    Arthur1610

    Joined:
    Dec 6, 2020
    Posts:
    22
    Hello. I tried to make a score and high score for my game by using Player Prefs but got the error CS0029 in the console.

    Here is my code :

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3.  
    4. public class Score : MonoBehaviour
    5. {
    6.     public Transform player;
    7.     public Text scoreText;
    8.     public Text highscoreText;
    9.  
    10.     void Start()
    11.     {
    12.         highscoreText.text = PlayerPrefs.GetInt("HighScore", 0).ToString();
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         int number = player.position.z.ToString("0");
    18.         scoreText.text = number.ToString();
    19.  
    20.         if (number > PlayerPrefs.GetInt("HighScore", 0))
    21.         {
    22.             PlayerPrefs.SetInt("HighScore", number);
    23.             highscoreText.text = number.ToString();
    24.         }
    25.     }
    26. }
    Here is also the error message:

    Assets\Scripts\Score.cs(17,22): error CS0029: Cannot implicitly convert type 'string' to 'int'

    Thank you in advance.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Line 17, char 22 is what the error points to. But to be clear, you are doing ToString, but trying to assign the value to an int.
    ToString will convert player.position.z into a string not an int.
     
  3. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    497
    You were already given the answer in the other topic that you posted in:
    https://forum.unity.com/threads/error-code-cs0029.898880/#post-6852347
    And you already said in that one that the solution provided works, so why have you put your script back to the old broken code again?
    On line 17 you have converted the z value to a string but are trying to put it into an integer.
     
    Kurt-Dekker and Brathnann like this.
  4. Arthur1610

    Arthur1610

    Joined:
    Dec 6, 2020
    Posts:
    22
    Ok. Thank you.
     
    Last edited: Feb 19, 2021
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    DEFINITELY start from a tutorial because trying random things isn't likely to succeed.

    Screen Shot 2021-02-19 at 7.21.47 AM.png