- Home /
string comparisons
Hi guys, to fix my last problem i changed everything to a string but now i have a problem i cant use >, = ,>= i cant use comparisons,how do i fix this?thanks
Do you need to compare string lengths? If so, use String.Length.
if you are using string.Compare, you should be able to evaluate equal, less, or greater from the output as str1 and str2 are compared.
string your$$anonymous$$om = "Bobbi";
string yourDad = "Bobby";
if (string.Compare(your$$anonymous$$om, yourDad) == 0)
Debug.Log("$$anonymous$$y $$anonymous$$om is $$anonymous$$y Dad");
else
Debug.Log("$$anonymous$$y $$anonymous$$om is NOT my Dad"); // <-- thats the one
if it's less than 0, in the case above, str1(yourmom) is less than str2(yourdad), greater than is just the opposite.
If your experience is an integer, then for the love of all that is natural in the world, use it(cast whatever) as an int and stop trying to work around your issues, just like the experience issues you're having from YOUR php script that you have posted twice about in the last two days.
Here: http://answers.unity3d.com/questions/748528/receiving-xp-but-not-setting-it.html And Here: http://answers.unity3d.com/questions/748068/not-receiving-exp.html
Answer by fafase · Jul 15, 2014 at 12:43 PM
Use http://msdn.microsoft.com/en-us/library/zkcaxw5y.aspx
The result is -1, 0 or 1 depending if the first is before, same or after.
string a = "Player1";
string b = "Player2";
print(String.Compare (a, b));
This will print -1 since the first is smaller than the second.
string a = "Phil";
string b = "Alan";
print(String.Compare (a, b));
This will print 1 since Phil is alphabetically after Alan.
Each char is compared until there is a mismatch, the result of it defines the return value.
EDIT:
int result = String.Compare(exp, expToLevel);
if( result < 0) // Not enough exp
else if (result == 0) // Just the right amount
else if (result > 0) // More than enough
Answer by Albert-han · Jul 15, 2014 at 01:47 PM
Posting in answer to let people see the lines.
This is my script now.On line 62 i need my ExpToLevel to be a string not an int but i already set it as a ExpToLevel int.
//public int CurLevel;
//public int NextLevel;
//public int ExpToLevel;
//public int Exp;
public int CurLevel;
public int NextLevel;
public int ExpToLevel;
public string Exp;
public static RankManager Inst;
public int MaxLevel;
public WeaponManager Player;
// Use this for initialization
void Start () {
Debug.Log ("Getting xp");
Exp = PlayerPrefs.GetString("PlayerScore");
Debug.Log ("Got xp");
//NextLevel = CurLevel + 1;
Inst = this;
}
// Update is called once per frame
void Update () {
//int xp;
// if (int.TryParse(Exp, out xp))
//Exp = PlayerPrefs.GetInt ("PlayerScore");
//Debug.Log ("Update xp");
if (NetworkManager.Instance.MyPlayer.PlayerName != "")
{
Player = NetworkManager.Instance.MyPlayer.Manager.FirstpersonCont;
foreach(Gun g in Player.Weapons)
{
if(CurLevel > g.UnlockLevel)
{
g.Unlocked = true;
}
else
{
g.Unlocked = false;
}
foreach(Sight s in g.Sights)
{
if(s.UnlockKills >= g.Kills)
{
s.Unlocked = true;
}
else
{
s.Unlocked = false;
}
}
}
}
if (CurLevel >= MaxLevel)
{
CurLevel = MaxLevel;
}
if (Exp >= ExpToLevel)
{
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");
}
}
}
}
And why you need the XP to be string ins$$anonymous$$d of Int? Really dont understand.
How will you handle the situation if the player gets more xp than he needs to level up and you need to handle the rest? I see absolutely no reason to handle it as String
If you want to store the xp in playerprefs you can store integer there using SetInt and GetInt ins$$anonymous$$d of the string version
Because for some reason when i use it as int i cant receive xp from database.
Your answer
![](https://koobas.hobune.stream/wayback/20220613154123im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiplying a string 1 Answer
Input string was not in the correct format 1 Answer
Please help get my code to work...compiler errors 2 Answers