Convert int to string and vice-versa
So I want to change this integer to a string, change a label's text value and change it back to an integer. But I get these errors:
Assets/Scripts/Gun/Gun.cs(116,24): error CS0029: Cannot implicitly convert type`int' to `string'
Assets/Scripts/Gun/Gun.cs(117,24): error CS0029: Cannot implicitly convert type `int' to `string'
Assets/Scripts/Gun/Gun.cs(118,21): error CS1502: The best overloaded method match for `int.Parse(string)' has some invalid arguments
Assets/Scripts/Gun/Gun.cs(118,21): error CS1503: Argument `#1' cannot convert `int' expression to type `string'
Assets/Scripts/Gun/Gun.cs(119,21): error CS1502: The best overloaded method match for `int.Parse(string)' has some invalid arguments
Assets/Scripts/Gun/Gun.cs(119,21): error CS1503: Argument `#1' cannot convert `int' expression to type `string'
this is not the entire script:
private int MAGBULLET;
public GUIText AmmoUi;
public GUIText VestUi;
private int VESTBULLET;
private void UpdateAmmoOnScreen()
{
MAGBULLET = magazineBullets;
VESTBULLET = ammoInVest;
MAGBULLET.ToString();
VESTBULLET.ToString();
AmmoUi.text = MAGBULLET;
VestUi.text = VESTBULLET;
int.Parse (MAGBULLET);
int.Parse (VESTBULLET);
}
}
Answer by doublemax · Sep 12, 2016 at 04:37 PM
ToString() returns a string.
int.Parse() returns an int,
string mb_string = MAGBULLET.ToString();
string vb_string = VESTBULLET.ToString();
AmmoUi.text = mb_string;
VestUi.text = vb_string;
int mb_int = int.Parse (mb_string);
int vb_int = int.Parse (vb_string);
Ok, but do $$anonymous$$AGBULLET and VESTBULLET stay as integers?
Yes, a variable never changes its type.
ToString() doesn't mean that the variable is converted into a string. It just returns a string representing the integer.
Your answer
Follow this Question
Related Questions
How to round to 2 decimals with format? (123456 > 123K > 123.45K) (C#) 0 Answers
Changing GUI Text to int and applying operands 1 Answer
Convert a found gameobject's name to a string 1 Answer
How to count down by mouse click? 1 Answer
Having trouble converting this int to string, can someone help? 0 Answers