0
\$\begingroup\$

Description

I want my character prefabs to come with their own health bar, but I can't simply add one as part of the prefab, because it's a UI element and therefore goes into a Canvas.

Potential Solution

I could write a script that takes in a separate health bar prefab and instantiates it when the character prefab's instantiated, but it's one extra script.

Is there a more conventional way to do this? I'd think this is a common problem.

\$\endgroup\$
  • \$\begingroup\$ As the UI element isn't directly part of the character (in a unity sense: they share no direct transform relation) it is not common practice to do so. Even if you did, you'd have to script a parent reassignment to a Canvas, which would be an additional script either way. \$\endgroup\$ – Weckar E. Oct 17 at 1:20
  • \$\begingroup\$ Also, for the sake of exploring an answer, how does this health bar appear on the UI? \$\endgroup\$ – Weckar E. Oct 17 at 1:22
  • \$\begingroup\$ Why you don't want the character to have its own ui canvas? If you use one single canvas for everything then your whole ui will be "recalculated" each time a single healthbar changes. \$\endgroup\$ – Nikaas Oct 18 at 11:31
  • \$\begingroup\$ @WeckarE. I'm not quite sure what you mean. It just... sits under the character? And follows the character's movements, of course. \$\endgroup\$ – verified_tinker Oct 18 at 11:35
  • \$\begingroup\$ @Nikaas I didn't know that! I thought the fewer the canvases, the better. Still, there's going to be a good number of characters on-screen (up to about 30). Should I still go for that? \$\endgroup\$ – verified_tinker Oct 18 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.