- Home /
ExpToLevel not increasing
Hi guys,i have been using this script for leveling in the game but i tried to simplify it to make it shorter then after i did that,my leveling stopped working.
NewScript:
using UnityEngine;
using System.Collections;
using System;
public class RankManager : MonoBehaviour {
public int CurLevel;
public string 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 () {
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;
}
}
}
}
}
void MultiplyExperience(){
if (CurLevel >= MaxLevel)
{
CurLevel = MaxLevel;
}
int Compare = string.Compare(Exp, ExpToLevel);
Debug.Log ("Compared");
if (Compare == 0);
else
if (Compare > 0) // More than ExpToLevel
{
CurLevel++;
int exptolevel = Convert.ToInt32(ExpToLevel);
exptolevel *= 2;
ExpToLevel = exptolevel.ToString();
}
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");
}
}
}
Old Script:
//public int ExpToLevel;
//public int Exp;
public int CurLevel;
public int NextLevel;
public string ExpToLevel;
public string Exp;
public static RankManager Inst;
public int MaxLevel;
public WeaponManager Player;
int i;
// Use this for initialization
void Start () {
Debug.Log ("Getting xp");
Exp = PlayerPrefs.GetString("PlayerScore");
Debug.Log ("Got xp");
//NextLevel = CurLevel + 1;
Inst = this;
if (int.TryParse(ExpToLevel, out i));
}
// 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;
}
//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;
int exptolevel = Convert.ToInt32(ExpToLevel);
exptolevel *= 2;
ExpToLevel = exptolevel.ToString();
//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");
}
}
}
}
ExpToLevel is not doubling when my exp is higher than it.
A quick comment, throughout the last couple of days and because of the free hosting/php/mysql service you use... you have created one of the nastiest kludges i've seen in a long time. You really should workout your data store issues (returning an int from mysql that is wrapped in various ad crap from the free host) and code to the actual types. You've created your own mess by trying to work around EVERYTHING ins$$anonymous$$d of doing it remotely "more correctly". Just my 2 cents.
I think it would be a lot easier to store Exp and ExpToLevel as ints.
int Exp = int.TryParse(PlayerPrefs.GetString("PlayerScore"));
then when you save back to "PlayerScore"
PlayerPrefs.SetString("PlayerScore", ""+Exp); //Or
PlayerPrefs.SetString("PlayerScore", Exp.ToString());
This allows you to work only with ints while still storing it as a string. The math should be a lot easier to work out, test, debug, etc.
Your answer
![](https://koobas.hobune.stream/wayback/20220613154232im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
String Multiplying 1 Answer
Another error 0 Answers
Not receiving exp 1 Answer
Unexpected Token: if 2 Answers
unexpected Token error 1 Answer