Reputation: 1
I've attached the following script to a Game Object named "floatingText"
This works but I want to activate the floating text only when an object is destroyed. I have another Destroy script attached to a sphere/bullet that is fired by pressing a button on screen to destroy a Cylinder object.
My question is how to get the floating text to show only when the Cylinder is destroyed by the bullet at the location of the impact.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Float : MonoBehaviour
{
Text text;
public float fadeDuration = 2.0f;
public float speed = 2.0f;
// Use this for initialization
void Start ()
{
text = this.GetComponent<Text>();
StartCoroutine(Fade());
}
public IEnumerator Fade ()
{
float fadespeed = (float)1.0 / fadeDuration;
Color c = text.color;
for (float t = 0.0f; t < 1.0f; t += Time.deltaTime * fadespeed)
{
c.a = Mathf.Lerp(1, 0, t);
text.color = c;
yield return true;
}
Destroy(this.gameObject);
}
// Update is called once per frame
void Update ()
{
this.transform.Translate(Vector3.up * Time.deltaTime * speed);
}
}
The script that controls the destroying of the object:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Destroy : MonoBehaviour
{
public ParticleSystem effect;
public AudioSource explosion;
public Text scoreText;
public GameObject floatingText;
void OnCollisionExit(Collision col)
{
if (col.gameObject.tag == "Cylinder")
{
effect.Play();
explosion.Play();
Destroy(col.gameObject);
}
}
}
Upvotes: 0
Views: 256
Reputation: 90749
Simply disable the object in Start
and enable it later:
public class Float : MonoBehaviour
{
Text text;
public float fadeDuration = 2.0f;
public float speed = 2.0f;
// Use this for initialization
void Start ()
{
text = this.GetComponent<Text>();
// Hide object
gameObject.SetActive(false);
}
public ShowTextAndFade(Vector3 initialPosition)
{
StartCoroutine (Fade(initialPosition));
}
private IEnumerator Fade (Vector3 initialPosition)
{
// Show object
gameObject.SetActive(true);
// Set position
transform.position = initialPosition;
float fadespeed = (float)1.0 / fadeDuration;
Color c = text.color;
for (float t = 0.0f; t < 1.0f; t += Time.deltaTime * fadespeed)
{
c.a = Mathf.Lerp(1, 0, t);
text.color = c;
yield return true;
}
Destroy(this.gameObject);
}
// Update is called once per frame
void Update ()
{
this.transform.Translate(Vector3.up * Time.deltaTime * speed);
}
}
and call it in the Destroy
script. I asume you want to reuse that so I'ld use a prefab:
floatPrefab
of the Destroy
component(s)In Destroy
:
// Reference the prefab in the inspector
public Float floatPrefab;
//...
effect.Play();
explosion.Play();
var position = col.transform.position;
Destroy(col.gameObject);
// Spawn the float text
var float = Instantiate(floatPrefab);
// And start fading
float.ShowTextAndFade(position);
You can also skip the prefab part
delete the Instantiate
but instead use
public Float float;
//...
float.ShowTextAndFade(position);
Upvotes: 2
Reputation: 1074
The floating text should be made into a prefab so that it can be instantiated when the cylinder is destroyed using the OnDestroy
event. Once instantiated, you can pass information from your cylinder to the instantiated floating text through the floatingText
GameObject.
...
public string textToShow = "Floating text you want to show";
public Vector3 pointOfCollision = gameObject.transform.position; // set on impact
void OnDestroy()
{
GameObject floatingText = Instantiate(floatingTextPrefab, pointOfCollision, floatingTextPrefab.transform.localRotation);
floatingText.Text.text = textToShow;
StartCoroutine(floatingText.GetComponent<Float>().Fade());
}
Upvotes: 1