Hide (not delete) text after given time..?
Hey guys, well I've read through almost every single post I could find through Google, but it just isn't working out.. I don't get it and 90% of the answer are fixed with JS or from 20XX < 2017.. which is really weird.
The following BoostPowerup class is giving my PlayerCharacter a nice little speed boost in a 2D sidescroller and its working, BUT, now I want to display a TextMeshPro Label saying "Boost activated!" for 3 seconds. I've tried doing it with deltaTime and subtracting something from a local variable to get a timer, I tried IEnumerators and Coroutines, I tried almost everything, but I think I am missing something.. It shows the text, but it never goes away again. It just stays there.
public class BoostPowerup : Collectible {
[Range(1, 3)] public float boostMultiplicator = 1;
public float boostTime = 3.0f;
public GameObject PowerUpInfo;
public GameObject powerUpInfoText;
private TextMeshProUGUI powerUpInfoText2;
private bool active;
protected override void OnPickUp(PlayerCharacter player) {
player.Boost(boostMultiplicator, boostTime);
DisplayInfoText();
StartCoroutine(DisableInfoTextAfterSeconds(3, PowerUpInfo));
}
private void DisplayInfoText() {
PowerUpInfo.SetActive(true);
powerUpInfoText2 = powerUpInfoText.GetComponent<TextMeshProUGUI>();
powerUpInfoText2.text = "Boost activated!";
}
private IEnumerator DisableInfoTextAfterSeconds(int seconds, GameObject obj) {
yield return new WaitForSeconds(seconds);
obj.SetActive(false);
powerUpInfoText2.text = "";
}
}
Any help is greatly appreciated, thanks in advance dear Unity-friends!
Why do you feed the object "PowerUpInfo" into the "DisableInfoTextAfterSeconds" function ins$$anonymous$$d of "powerUpInfoText"?
Also does your script call that function at all? $$anonymous$$aybe add a "Debug.Log("DisableInfoTextAfterSeconds called");" to the top of the function?
Answer by Sgt_Spike · Jul 29, 2018 at 09:46 PM
Hey there. I'm not too great with parameters myself so I'm not too sure what the problem with the coroutine is. Would it not be easier to have something like this:
public class BoostPowerup : Collectible {
[Range(1, 3)] public float boostMultiplicator = 1;
public float boostTime = 3.0f;
public GameObject PowerUpInfo;
public GameObject powerUpInfoText;
private TextMeshProUGUI powerUpInfoText2;
private bool active;
protected override void OnPickUp(PlayerCharacter player) {
player.Boost(boostMultiplicator, boostTime);
DisplayInfoText();
}
private void DisplayInfoText() {
PowerUpInfo.SetActive(true);
powerUpInfoText2 = powerUpInfoText.GetComponent<TextMeshProUGUI>();
powerUpInfoText2.text = "Boost activated!";
StartCoroutine(DisableInfoTextAfterSeconds());
}
private IEnumerator DisableInfoTextAfterSeconds() {
yield return new WaitForSeconds(3f);
obj.SetActive(false);
powerUpInfoText2.text = "";
}
}
Not sure if this is what you're looking for but see if that works.
Sadly this doesn't change anything... Just tried it out, plus line 27 needs to be PowerUpInfo.SetActive(false); then, but sadly nothings changed...
Hmm, I've never used Text$$anonymous$$eshProUGUI before so maybe that's what the problem is. What is you tried hiding the text by using SetActive ins$$anonymous$$d of .text = ""? Looking at your code it looks like powerUpInfoText is the gameobject for your text, so maybe replace 'powerUpInfoText2.text = "";' with 'powerUpInfoText.SetActive(false);'. See if that does anything good.