How to attach text to moving prefab so that both move together?
Hi, I am new to Unity and got started with a space shooter 2d tutorial on YouTube. I wanted to modify it so that a text is displayed on each enemy. How can I do this?
Answer by Ninjapro2k_ · Dec 20, 2016 at 08:03 PM
Try childing a the text canvas to your enemy prefab :)
You could also write a short script like
public class Example : Monobehavior {
public Transform target;
//Or
public GameObject target;
void Update () {
transform.position = (target.transform.position + offset);
public Vector2 offset = new Vector2 (example x, example y)
}
}
Where either target would be the enemy prefab. I don't know if transform.position works for text though. If it does it will probably have to be set to world space in the inspector^^
Oh, and offset can be adjusted to preference in the inspector as long as it's public :)
Your answer
Follow this Question
Related Questions
How can I have Unity display the correct tiles based on their surrounding tiles (ie corners) 2 Answers
Adventure Game Help 0 Answers
How to assign a canvas's text to a prefab? 1 Answer
.enabled = false not working 1 Answer
Adding Text to Prefab 0 Answers