- Home /
This question does not respect the rules of Unity Answers : Posting about a specific compiling error or NullReferenceException
Cant convert int to string in c#
Here is my code:
public class checklevelcheckskill : MonoBehaviour {
public string PlayerSKillPointsS;
public int RequiredSkillLevelForUpgrade;
public int PlayerSkillPointsInInt;
//BUttons and Menus
public GameObject SkillPointsWarningMenu;
//Written Variables
// Use this for initialization
public void Start ()
{
RequiredSkillLevelForUpgrade = 0;
SkillPointsWarningMenu.active = false;
}
// Update is called once per frame
void Update ()
{
PlayerPrefs.GetString("PlayerSkillPoints");
PlayerSKillPointsS = PlayerPrefs.GetString("PlayerSkillPoints");
PlayerSkillPointsInInt = PlayerSKillPointsS;
}
}
Im getting this error: Cannot implicitly convert type 'string' to 'int' And when I changed my script to this,
public class checklevelcheckskill : MonoBehaviour {
public string PlayerSKillPointsS;
public int RequiredSkillLevelForUpgrade;
public int PlayerSkillPointsInInt;
//BUttons and Menus
public GameObject SkillPointsWarningMenu;
//Written Variables
// Use this for initialization
public void Start ()
{
RequiredSkillLevelForUpgrade = 0;
SkillPointsWarningMenu.active = false;
}
// Update is called once per frame
void Update ()
{
PlayerPrefs.GetString("PlayerSkillPoints");
PlayerSKillPointsS = PlayerPrefs.GetString("PlayerSkillPoints");
PlayerSkillPointsInInt.ToString() = PlayerSKillPointsS;
}
}
Now Im getting this error: The left-hand side of an assignment must be a variable, property or indexer
I just want first variable to be equal to second one. PlayerSkillPointsInInt = PlayerSKillPointsS;
Are there anybody knows how to solve this problem? Thanks For Your Help.
Answer by tanoshimi · Jul 24, 2016 at 06:47 AM
Your title says convert int to string, which is done trivially using the ToString() method. However, your question is actually about converting string to int, which must be done slightly differently since not every string can be represented as an int. Try this :
int.TryParse(PlayerSKillPointsS, out PlayerSkillPointsInInt);
@tanoshimi : Please, don't forget the rules of Unity Answers : FAQ
For this kind of question, leave a comment in the moderation queue and reject the question right after. Unity Answers is full of off-topic questions like this one. This is a C# problem, not a Unity-related one.
@Hellium - I can't leave comments on posts in the mod queue any more, and there was also a rule about not rejecting posts without an explanation :( But yeah, you're right - this one could probably have been rejected. To be fair to the OP, the code was formatted correctly, the error message was stated in full, and they had tried to make a change to fix the error themselves. And they were polite. :)
Follow this Question
Related Questions
Having a GUI text as a int 3 Answers
Getting string to int then var 1 Answer
A string converted from Json cant become "smart" and understand variables in my code, true or false? 1 Answer
String can't be converted to float or int? 1 Answer
How can I make a Dropdown option to be converted on a text to appear somewhere? 0 Answers