- Home /
String Multiplying
Hi guys i have a problem.I have set all some of my int into string to work with another script(only chance for the script to work) and now i cant multiply the value of that string so i converted the involved one into a int and found another way.
I need to change that int into a string for that function only and change it back when its done.Then i cant multiply it however i want.
if (CurLevel >= MaxLevel)
{
CurLevel = MaxLevel;
}
int ExpToLevel : String;
ExpToLevel = ExpToLevel.ToString();
int result = string.Compare(Exp, ExpToLevel);
if (result == 0);
else if (result > 0) // More than enough
{
CurLevel++;
NextLevel = CurLevel + 1;
//ExpToLevel *= 2;
if(Network.peerType != NetworkPeerType.Disconnected)
{
NetworkManager.Instance.MyPlayer.Manager.networkView.RPC("UpdateRank",RPCMode.All,CurLevel);
PlayerPrefs.GetString("PlayerScore" + NetworkManager.Instance.PlayerName, Exp);
Debug.Log("RankManager Getting Int");
}
}
}
}
I need the (//ExpToLevel to be an int on line 13) but need it to be string on line (7) if i dont convert it to string my other scripts wont work because i tried int and dint work.Here's my other thread (http://answers.unity3d.com/questions/748068/not-receiving-exp.html).thanks
Answer by dsada · Jul 16, 2014 at 10:20 AM
As I wrote in your previous discussion you dont need your ExpToLevel variable to be string. Make it and Integer. You cannot make math operation with strings its not what it made for.
int difference = ExptToLevel - Exp;
if(difference <= 0) //more than enough or just enough
{
++CurLevel; //increase level
Exp = difference //add the rest. It will be 0 if he collected just enogh exp
}
This is an obvious thing to use Integer and not string
$$anonymous$$y exp needs to be a string unless my other script wont even receive the exp from database so i need to compare a sting to an int but i dont think it works that way so i converted both of them to strings to be compared and now i just cant multiply my string to go to next level.I thought of a way but i dont know if it is possible. I need Exptolevel(default in script) to be an int but when i need to compare it i change (exptolevel) to a string (temporarily)to compare with exp (string).
So, convert it into an int, do the math and put it back into string.
int expInt = Convert.ToInt32(Exp);
expInt *= value;
Exp = expInt.ToString();
You cannot do arithmetic on string, just like you cannot multiply your first name times carrot.
Ok thanks for the explanation so it should. Convert it to an int from string, $$anonymous$$ultiply it with the value, Change it to a string back?
If i add
int result = string.Compare(Exp, ExpToLevel);
if (result == 0);
else if (result > 0) // $$anonymous$$ore than enough
Will it work because i only need it to times the value when the exp is higher than exptolevel.
void $$anonymous$$ultiplyExperience(){
int result = string.Compare(Exp, ExpToLevel);
if (result > 0){
int expInt = Convert.ToInt32(Exp);
expInt *= value;
Exp = expInt.ToString();
}
}
i get this Assets/Scripts/Player/Rank$$anonymous$$anager.cs(76,43): error CS0103: The name `Convert' does not exist in the current context
Your answer
Follow this Question
Related Questions
Another error 0 Answers
Unexpected Token: if 2 Answers
Not receiving exp 1 Answer
ExpToLevel not increasing 0 Answers
unexpected Token error 1 Answer