Help! Combat scrolling text is fading out everything

Updated on November 13, 2019 in Answers
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
4 on November 11, 2019

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!

  1. public class CombatText : MonoBehaviour
  2. {
  3. private float speed;
  4. private Vector3 direction;
  5. private float fadeTime;
  6. // Update is called once per frame
  7. void Update ()
  8. {
  9. float translation = speed * Time.deltaTime;
  10. transform.Translate (direction * translation);
  11. }
  12. public void Initialize(float speed, Vector3 direction, float fadeTime)
  13. {
  14. Debug.Log ("Initialize called!");
  15. this.speed = speed;
  16. this.direction = direction;
  17. this.fadeTime = fadeTime;
  18. StartCoroutine ("FadeOut");
  19. }
  20. private IEnumerator FadeOut()
  21. {
  22. float startAlpha=GetComponent<Text>().color.a;
  23. float rate = 1.0f/fadeTime;
  24. float progress = 0.0f;
  25. while(progress<1.0f)
  26. {
  27. Color tmpColor = GetComponent<Text> ().color;
  28. GetComponent<Text> ().color = new Color (tmpColor.r, tmpColor.b, tmpColor.g, Mathf.Lerp(startAlpha,0,progress));
  29. progress += rate * Time.deltaTime;
  30. yield return null;
  31. }
  32. Destroy (gameObject);
  33. }
  34. }
  35. public class CombatTextManager : MonoBehaviour

{ public GameObject CombatTextPrefab; public Transform CombatDisplayparent; public float speed; public Vector3 direction; public float fadeTime;

  1. private static CombatTextManager instance;
  2. public static CombatTextManager Instance
  3. {
  4. get
  5. {
  6. if (instance == null)
  7. {
  8. instance = GameObject.FindObjectOfType<CombatTextManager> ();
  9. }
  10. return instance;
  11. }
  12. }
  13. public void CreateText(string text, Color color)
  14. {
  15. Debug.Log ("Hello? Am I being called?");
  16. GameObject ct=Instantiate (CombatTextPrefab);
  17. ct.transform.SetParent(CombatDisplayparent);
  18. ct.GetComponent<CombatText> ().Initialize(speed, direction, fadeTime);
  19. ct.GetComponent<Text> ().text = text;
  20. ct.GetComponent<Text> ().color = color;
  21. }
  • Liked by
Reply
3 on November 11, 2019

Where are you setting your CombatTextPrefab? Is it a prefab reference, or is it a scene reference?

on November 12, 2019

It’s a prefab reference. Line 17 of script 2

 

Wise
on November 12, 2019

Right, but where are you setting it?

In the scene, another script?

on November 13, 2019

Second script line 42 and it gets assigned on lines 18,19,20,21.

Show more replies
  • Liked by
Reply
Cancel