Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by KwahuNashoba · Apr 07, 2015 at 12:40 AM · unity 5uidestroyhealthbarhealth bar

Destroying UI elements problem

Hello there,

I am trying to build floating health bar which follows enemies and to do that I created a prefab based on UI Slider object. In my script that controls enemy health I have public GameObject healthBar; variable which is associated with this prefab. Good to mention that after creating this healthBar it is set to be child of my HUD canvas.

And now comes the problem!

When I kill enemy I call:

 Destroy(healthBar);

I also tried:

 Destroy(healthBar.gameObject);

but it simply wont destroy it.

I solved it by adding Remove function to script which controls health bar and does exactly the same (calling Destroy(gameObject)); and it worked.

My question is why the first approach isn't doing the thing?

Thanx

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DoTA_KAMIKADzE · Apr 08, 2015 at 01:40 AM 0
Share

All of the above should work from anywhere so far that your "healthBar" or "helthBar" references the correct health bar.

Try adding a unique health bar to your enemy with a unique tag or any other unique value and in the place where you call Destroy call Debug.Log(healthBar.someUniqueValue);

avatar image KwahuNashoba · Apr 09, 2015 at 06:35 PM 0
Share

Sorry about my spelling err :D I tried Debug.Log(healthBar.GetComponent().maxValue); and it logged 1 which is default value of prefab and not the value I set latter. This led me to the source of problem which is:

What I was doing is dragging and dropping healthBar prefab to my public GameObject healthBar; . And when I call Instantiate I didn't save the return value to my healthBar variable. This means that I was calling destroy on prefab and not the instance of that prefab? Am I right? I'm new to this Unity stuf :) I'm a bit confused now cause I do something like this:

 healthBar = (Instantiate (healthBar, transform.position, Quaternion.identity) as GameObject);

I use same variable to instantiate object and to save that object back. This works but I'm not sure what happens under the hood?

And thanx for help :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by KitoCode · Apr 09, 2015 at 04:53 PM

Your code seems logically, my only theory is that you have healthBar linked to the wrong object. For example, you have it linked to the prefab not the in game object.

Try gaining access to it at run time, by either finding it via GameObject.Find(""); or simply by making HealthBar GameObject public or [SerializableField] in your inspector and dragging and dropping the exact object you want destroyed.

Another advice I can offer, is try destroying something else, or if it is a UI 4.6 problem. Perhaps make HealthBar a child of an empty object, then destroying that object.

Hope this helps.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KwahuNashoba · Apr 09, 2015 at 06:36 PM 0
Share

For example, you have it linked to the prefab not the in game object.

Yes, it was exactly this! Thanx!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

I need help with the player's health points 1 Answer

UNET - UI Not updating with Healthbar and other bars. 1 Answer

Cannot drag text object into inspector 2 Answers

Why do i get these messages from Unity3D UI? 6 Answers

Ui image appear and disappear on trigger 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges