- Home /
Making a level up button.,
I'll try and explain as best I can, still think of myself as a beginner. So I have created a button that appears upon attaining a number of experience points, clicking the button causes the level up points(hp etc) to be applied and then the button disappears only to be called again upon next level up.
===========================================================
public class GlobalLevel : MonoBehaviour
{
public static int currentLevel = 1;
public int InternalLevel;
public bool levelupDeActive = false;
public bool gainedLevel = false;
public GameObject levelupBtn;
public GameObject buttonPanel;
// Update is called once per frame
void Update()
{
InternalLevel = currentLevel;
if (GlobalXp.currentXp >= 20)
{
currentLevel = 2;
if (levelupDeActive == false)
{
buttonPanel.SetActive(true);
}
}
if (GlobalXp.currentXp >= 40)
{
currentLevel = 3;
gainedLevel = true;
if (levelupDeActive == false)
{
buttonPanel.SetActive(true);
}
}
if (gainedLevel == true)
{
levelupDeActive = false;
}
}
public void LevelUP()
{
PLayerPerks.PerkPoints += 1;
HealthMonitor.MaxHealth += 50;
// GlobalXp.currentXp += 1;
levelupDeActive = true;
buttonPanel.SetActive(false);
}
}
==============================================================
It works fine for the first level up (level 1 to level 2) but the problem is that once it is turned to false within update, upon obtaining enough xp for level 3, it simply keeps setting the bool back every frame. I need a way of
if levelup has been achived(xp has reached 40) thepanel becomes active until the button is clicked click button, gain stats,now stay inactive until next xp milestone.
First time asking a question so sorry if its a bit all over the place, kinda hoping that typing this out I might figure it out myself :P
thanks for any help.
,
I'm sorry about the post format, no idea what I did :P
Answer by sadowlight123 · Sep 13, 2019 at 10:49 AM
I wrote for you a more general technique for levels.
Make a custom data type (class) called level , that will include all properties (that are the same for all levels).
Make a list of level called levels
Each time check for the level that is your currentlevel + 1 for the neededXP
Once the XP is >= then my currentlevel + 1 neededXP , then show the panel Once the button pressed level up as usual.
The draft code:
public class button : MonoBehaviour
{
public int currentXp;
public int currentLevel = 1;
public GameObject levelupBtn;
public GameObject buttonPanel;
[System.Serializable]
public class level {
public int number;
public int neededXP;
//add all needed properties of a level maybe like combat points, etc.
}
public List<level> levels;
// Update is called once per frame
void Update()
{
checkStates();
}
void checkStates()
{
if (currentLevel < levels.Count)
{
if (currentXp >= levels[currentLevel].neededXP)
{
buttonPanel.SetActive(true);
}
}
}
public void LevelUP()
{
// do whatever you want here
currentLevel++;
buttonPanel.SetActive(false);
}
}
Answer by kris_absolution · Sep 13, 2019 at 01:01 PM
I thought about it for a while and actually managed to fix it myself:
If (GlobalXp.currentXp >= 20 && GlobalLevel.currentLevel == 1)
{
buttonPanel.SetActive(true);
}
if (GlobalXp.currentXp >= 40 && GlobalLevel.currentLevel == 2)
{
buttonPanel.SetActive(true);
}
}
public void LevelUP()
{
//increase level after button is clicked so its not repeated in update.
GlobalLevel.currentLevel += 1;
PLayerPerks.PerkPoints += 1;
HealthMonitor.MaxHealth += 50;
buttonPanel.SetActive(false);
}
Probably not as efficient but I'm learning \0/.
Thanks for the reply though, I will probably convert it to something similar later on when I have learned more about (lists) etc.
Your answer
Follow this Question
Related Questions
Coroutines and if statements 1 Answer
What am I doing wrong with this bool? 3 Answers
Update collisions 1 Answer
Most efficient way of pausing a function 0 Answers