- Home /
Help! Combat scrolling text is fading out everything
I am setting up a panel where I want to scroll the text from combat, such as Player misses or Player hits slime for 20 damage. The text prefab is being instantiated and put in a panel. The trouble I'm having is that instead of each individual clone fading it, they all fade out at the same time. Not sure how to fix it, so some help would be appreciated. Thanks!
public class CombatText : MonoBehaviour
{
private float speed;
private Vector3 direction;
private float fadeTime;
// Update is called once per frame
void Update ()
{
float translation = speed * Time.deltaTime;
transform.Translate (direction * translation);
}
public void Initialize(float speed, Vector3 direction, float fadeTime)
{
Debug.Log ("Initialize called!");
this.speed = speed;
this.direction = direction;
this.fadeTime = fadeTime;
StartCoroutine ("FadeOut");
}
private IEnumerator FadeOut()
{
float startAlpha=GetComponent<Text>().color.a;
float rate = 1.0f/fadeTime;
float progress = 0.0f;
while(progress<1.0f)
{
Color tmpColor = GetComponent<Text> ().color;
GetComponent<Text> ().color = new Color (tmpColor.r, tmpColor.b, tmpColor.g, Mathf.Lerp(startAlpha,0,progress));
progress += rate * Time.deltaTime;
yield return null;
}
Destroy (gameObject);
}
}
public class CombatTextManager : MonoBehaviour
{ public GameObject CombatTextPrefab; public Transform CombatDisplayparent; public float speed; public Vector3 direction; public float fadeTime;
private static CombatTextManager instance;
public static CombatTextManager Instance
{
get
{
if (instance == null)
{
instance = GameObject.FindObjectOfType<CombatTextManager> ();
}
return instance;
}
}
public void CreateText(string text, Color color)
{
Debug.Log ("Hello? Am I being called?");
GameObject ct=Instantiate (CombatTextPrefab);
ct.transform.SetParent(CombatDisplayparent);
ct.GetComponent<CombatText> ().Initialize(speed, direction, fadeTime);
ct.GetComponent<Text> ().text = text;
ct.GetComponent<Text> ().color = color;
}
}
Comment
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Audio won't fade out. 1 Answer