Reputation: 23
I'm pretty new to coding, been learning for the past year and I'm currently working on an assignment for school and i can't figure out this bit of code for the love of my life.
I have an item that when the player interacts with it, executes this:
void Update ()
{
if (isPlayerNear && Input.GetKeyDown(KeyCode.E) && Avatar.strenghtAttribute >= 2f)
{
levelUp.LevelUp();
Destroy(gameObject);
}
My level up function is basically this:
public void LevelUp()
{
playerLevelText.text = ("You have gained a level!");
strenghtAttribute++;
intellectAttribute++;
playerLevel++;
}
I'm trying to figure out how to make playerLevelText.Text appear on the screen but only appear for a few seconds and I can't figure out how to make that work. Would anyone be kind enough to give me a hand?
Upvotes: 2
Views: 1609
Reputation: 4343
You could either set the text to be blank, or enable/disable the text object. I'd recommend using a coroutine for this.
void Update ()
{
if (isPlayerNear && Input.GetKeyDown(KeyCode.E) && Avatar.strenghtAttribute >= 2f)
{
levelUp.InitializeLevelUp());
Destroy(gameObject);
}
Since you are destroying the gameobject calling the coroutine, the coroutine will stop. A workaround is to call a normal function in your other script, which then calls the coroutine, so execution stays within the one script (there might be a cleaner way to do this).
public void InitializeLevelUp()
{
StartCoroutine(LevelUp());
}
public IEnumerator LevelUp()
{
playerLevelText.text = ("You have gained a level!");
strenghtAttribute++;
intellectAttribute++;
playerLevel++;
yield return new WaitForSeconds(2f);
playerLevelText.text = "";
//alternatively, set the text object inactive
}
Upvotes: 1