- Home /
Help with displaying gained EXP on screen.
I'm trying to write a script that displays the amount of exp points the player has earned in the last 3.5 seconds on the screen using a script. I managed to create a script that showed the exp they just gained but this overwrites the exp they gained beforehand. For example, if i earned 20 exp, then 10 more within a second, it will just display 10. I;m trying to code something so it would stack that 20 and 10, display it and if no more exp is earned within a few more seconds, it will reset back to 0 and hide.
public TMPro.TMP_Text expIncrementUI;
private void Start()
{
GameObject player = GameObject.Find("Player");
PlayerData playerData = player.GetComponent<PlayerData>();
previousCurrentEXP = playerData.currentEXP;
}
void OnGUI()
{
EXPIncrement();
}
void EXPIncrement()
{
GameObject player = GameObject.Find("Player");
PlayerData playerData = player.GetComponent<PlayerData>();
if (playerData.currentEXP != previousCurrentEXP)
{
StartCoroutine(EXPCombo());
}
IEnumerator EXPCombo()
{
GameObject player = GameObject.Find("Player");
PlayerData playerData = player.GetComponent<PlayerData>();
expGaining += playerData.currentEXP - previousCurrentEXP;
oldEXPGaining = expGaining;
expIncrementUI.enabled = true;
expIncrementUI.text = "+" + (expGaining).ToString();
yield return new WaitForSeconds(3.5f);
if (oldEXPGaining == expGaining)
{
expGaining = 0;
expIncrementUI.enabled = false;
previousCurrentEXP = playerData.currentEXP;
}
}
}
The error i am recieving is the fact this code keeps on adding exp after the first amount of exp is gained, so the numbers keep rising and rising and never stops.
Answer by Strixie13 · Jul 13, 2018 at 02:08 AM
Something like this maybe? I used two variables for EXP, a temporary and total. Every couple seconds the temporary one will dump into the total and reset. In the meantime the update function keeps updating the current EXP (unless its less than zero, in which case it will hide).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ExpExample : MonoBehaviour {
//When adding EXP for completing an objective add value to the temporaryEXP var, ie
//temporaryEXP += 6;
public int totalEXP; //Total EXP the character has.
public int temporaryEXP; //Temporary exp at 3.5 second intervals
bool displayingEXP;
public Text displayEXP; //Display text output.
private void Start()
{
displayingEXP = true; //Initialize Vars//
totalEXP = 0;
temporaryEXP = 0;
StartCoroutine(ResetEXP()); //Start Looping//
}
private void Update()
{
if(temporaryEXP > 0)
{
displayEXP.enabled = true;
displayEXP.text = temporaryEXP.ToString();
}
else
{
displayEXP.enabled = false;
}
}
public IEnumerator ResetEXP()
{
while(displayingEXP == true)
{
yield return new WaitForSeconds(3.5f);
totalEXP += temporaryEXP;
temporaryEXP = 0;
}
}
}
I’ll sure give this a try. Thank you for the response. After looking through it, I’ve noticed temporaryEXP never gets EXP added to it. I’m assu$$anonymous$$g temporaryEXP = newEXP - oldEXP. Correct me if I’m wrong or suggesting anything else, please.
No, you would need to do totalEXP += temporaryEXP. Temporary is to display in small segments, then you want to always add that to the total. Try plugging in numbers to help visualize it. If you have 100 exp already then gain 10, then gain 20, you have 30 temporary and 100 total. Display the 30 however you want, then your new total should be 130. Shouldn't be any $$anonymous$$uses in their anywhere.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Cooldowns with Time.time 2 Answers
Distribute terrain in zones 3 Answers
drop down list c# 1 Answer