This script will fadeout the gameobjects that are instantiated, except that it fades out and destroys ALL game objects not the individual gameobjects as expected. Do I need to name each one when they are instantiated?
First Script:
public class CombatTextManager : MonoBehaviour
{
public GameObject CombatTextPrefab;
public Transform CombatDisplayparent;
public float speed;
public Vector2 direction;
public float fadeTime;
public bool messageflag;
private static CombatTextManager instance;
public static CombatTextManager Instance
{
get
{
if (instance == null)
{
instance = GameObject.FindObjectOfType<CombatTextManager> ();
}
return instance;
}
}
public void CreateText(string message, Color color)
{
Debug.Log("Message is: " + message);
Debug.Log("Show text and fade");
GameObject ct = Instantiate (CombatTextPrefab);
ct.transform.SetParent (CombatDisplayparent);
ct.GetComponent<CombatText> ().InitializeFade (speed, direction, fadeTime);
ct.GetComponent<Text> ().text = message;
ct.GetComponent<Text> ().color = color;
return;
}
}
Second Script:
public class CombatText : MonoBehaviour
{
private float speed;
private Vector2 direction;
private float fadeTime;
void Update ()
{
float translation = speed * Time.deltaTime;
transform.Translate (direction * translation);
}
public void InitializeFade(float speed, Vector2 direction, float fadeTime)
{
this.speed = speed;
this.direction = direction;
this.fadeTime = fadeTime;
StartCoroutine ("FadeOut");
}
private IEnumerator FadeOut()
{
Debug.Log ("Fading Out...!");
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);
}
}
In case you need to see the call as well…
CombatTextManager.Instance.CreateText ("You Hit the" + EnemyNameText + "For " + damage, Color.black);