• 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
1
Question by Arlei · Aug 17, 2012 at 10:51 AM · objectrandomfunctionexplode

Function action applies to every object that have script with that function on it. Help please

Hi, I made script that causes that object that have that script explodes after some damage. Problem is, that i have many of these objects in scene end when one object takes the damage, one other in scene explodes. I don'n know where i made a mistake. Thanks for help.

Here is the script:

var maxHealth : float; static var currentHealth : float;

var dieSound : AudioClip; var dieVisual : GameObject;

function Start() { currentHealth = maxHealth; }

function Update() { if(currentHealth <= 0) { Destroy(); } }

function Destroy() { AudioSource.PlayClipAtPoint(dieSound, transform.position); Instantiate(dieVisual, transform.position, transform.rotation); Destroy(gameObject); }

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by ScroodgeM · Aug 17, 2012 at 10:52 AM

static variables are stored globally, not per instance. so make it non-static (remove static keyword)

Comment
Add comment · Show 8 · 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 Arlei · Aug 17, 2012 at 11:03 AM 0
Share

I removed it, but it didn't help..

avatar image ScroodgeM · Aug 17, 2012 at 11:13 AM 0
Share

you:

  1. removed static keyword from 2nd line of script

  2. saved script

  3. returned to unity and let unity recompile scripts

  4. run game again

  5. noticed that on one die all objects dies too?

avatar image Arlei · Aug 17, 2012 at 12:18 PM 0
Share

when there was static, all objects died at once, when i removed it, objects destroy one by one, but by order they are placed in hierarchy.

avatar image ScroodgeM · Aug 17, 2012 at 01:52 PM 0
Share

then try to localize point that starts destroying. before you'd remove static it was caused by all objects have shared health and once it becomes <=0, all objects are destroyed. now there's something else goes wrong.

check health of other objects - don't it goes to zero?

if they dies with sound and visual effects, something runs Destroy() method - where else this method can be raised?

etc

avatar image Arlei · Aug 17, 2012 at 03:42 PM 0
Share

It destroys by order in hirearchy only objects from the same prefab. I still didn't figured out what's the problem..

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException: Object reference not set to an instance of an object 1 Answer

What function to use, for this statement? 1 Answer

Random Drops 2 Answers

Better way to call function from another script from editor script? 1 Answer

For Loop isn't working properly! 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