Hi, I’ve been searching online, but can’t find the right answers. I want text to pop up whenever I get a bonus, saying +5000 or +100… I’m also trying to make it shrink and fade out after a second. This is what I have so far:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Script_CanvasScoreBonus : MonoBehaviour {
Text textBonus;
float textBonusAlpha;
void Start () {
textBonus = gameObject.GetComponent<Text>();
textBonusAlpha = textBonus.color.a;
textBonus.enabled = false;
}
public IEnumerator displayBonusEn (string displayText) {
textBonus.enabled = true;
textBonusAlpha = 1;
textBonus.text = displayText;
while (textBonusAlpha > 0) { textBonusAlpha -= .05f;
yield return null; }
textBonus.enabled = false;
}
}
I have this script attached to a text object called ‘Bonus Text’:
[34589-screen+shot+2014-10-31+at+12.32.50+pm.png|34589]
This script is then called when a player gets a bonus - which passes in a string like “+5000”. Everthing works - the “+5000” shows in my game at the right time, and it goes away after about a second - but it’s not fading out, it just goes away. It’s there when I need it, and completely gone in a second. I was initially doing -= Time.deltaTime * 2… Depending on what I put there - 2, 3, .5, it changes the length of time that the text stays up - if I put 2 than it lasts for a second, 3 lasts for a second and a half… I was initially not using a variable for ‘textBonusAlpha’ and just using textBonus.color.a instead, but I got this error: “Script_CanvasScoreBonus.cs(20,56): error CS1612: Cannot modify a value type return value of `UnityEngine.UI.Graphic.color’. Consider storing the value in a temporary variable” - Can’t seem to figure this out!
Also - could someone please tell me how to access the font size in the code? I know alpha is xxx.color.a - I want to decrease the font size gradually, as well. Thank u in advance!