- Home /
Show Enemy Name On UI Canvas?
Heyho! I am currently working on a 2D game and I try to make the health of the enemy appear on the UI rather than on top of the head of the enemy. I also want to make a public variable, that stores a name that I can give the enemy, and this should also show up once the enemy got hit. I tried following this tut; https://www.youtube.com/watch?v=_ICCSDmLCX4
But like I said, I rather want the text to appear on the UI screen. I also have no Idea on how to create a stringTotext for the name, especially If I want to make for every enemy a unique name (crate, gate, ghost..) Could somebody help me?
Answer by Llama_w_2Ls · Aug 26, 2020 at 06:40 PM
You could basically have a public string that equals the bame of the GameObject you hit. As long as the enemies' names are assigned as the namefield in the hierarchy (e.g. the player gameObject is called 'Player' and the ghost enemy is called 'Ghost in the hierarchy), you can get the name of the enemy you hit by getting the collider.gameObject.name in the OnCollisionEnter method. Assign the public string to the enemy name then assign the public UI text.text in your canvas to that string.
Thanks, i solved it! :)Though a bit differently, thanks for the input!
Your answer
Follow this Question
Related Questions
Raycast gun 2 Answers
Accessing Other Scripts Without Using Static Variables 1 Answer
How can I load a scene when the enemy hits the player? 5 Answers
Blood on weapon. 1 Answer
Points when an enemy dies. 2 Answers