- Home /
Change new GUI over time - 4.6
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':
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!
Answer by drod7425 · Oct 31, 2014 at 06:57 PM
You can do this pretty easily in 4.6 with UI transitions. You do not need to do this through code. This video explains it very well:
http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-transitions
Your answer
Follow this Question
Related Questions
Help with destroying guiRect? 0 Answers
Strange shader behaviour on UI component 0 Answers
v4.6 UI Element's Positioning Incorrect 1 Answer
Prefab as a Sprite 0 Answers