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 sandhillceltic · Mar 10, 2013 at 03:08 AM · javascriptdestroydeletehealth

Can someone explain the destroy () command?

I am working on a health system that involves the player taking damage when a creature hits him. I began researching the different script references that I would be able to use, and I came across Object.Destroy () (http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html) I was confused on how to use that command, to simply delete a game object after a certain number of hits, but the Object.destroy command is not working!

Can someone please explain how to use this reference, and give me a few examples on how to use it to delete game objects?

JavaScript only, please

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 AlucardJay · Mar 10, 2013 at 03:08 AM 0
Share

In regard to but the Object.destroy command is not working : Can you show us your current script please, thanks.

http://video.unity3d.com/video/7720450/tutorials-using-unity-answers

avatar image sandhillceltic · Mar 10, 2013 at 03:24 AM 0
Share

I haven't started the script yet, I came here to do some clarifying first. If I said like OnCollisionEnter, and the other object's name is enemy, how would I use Destroy () to delete this object, like can you tell me how I would use this to delete a gameobject.

I am planning to apply this to a projectile, so that when the projectile hits an object (such as an enemy) with the specified Gameobject name, the enemy will dissapear. I'm not looking for a health system, just a "one hit kill" system. If you really are confused on what I want, I will attempt to write a script. I have very little Javascript knowledge, So be prepared.

Can you just post a simple example, like "if (trigger) happens, delete (this) object.

thanks in advance

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Statement · Mar 10, 2013 at 03:39 AM

I haven't started the script yet... ...but the Object.destroy command is not working!

:)

Anyhow, if you want to destroy a game object then your first task is to get a reference to the object. If you know that your game object is called "Enemy" and you have no reference to the game object yet, you can use GameObject.Find("Enemy") to obtain it.

 var enemyGameObject = GameObject.Find("Enemy");

Then to destroy it, just call Destroy(enemyGameObject).

So your code should probably look something like this when you want to destroy that game object:

 var enemyGameObject = GameObject.Find("Enemy");
 Destroy(enemyGameObject);

Note that looking game objects up by name can be a problem. Consider what should/would happen if you had 3 enemies, all called "Enemy" for example.

A better solution might be to grab the game object from the object you are colliding with (or however you detect hits) using Destroy(colliderOrScriptOfEnemy.gameObject);

Comment
Add comment · Show 3 · 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 Kleptomaniac · Mar 10, 2013 at 04:03 AM 2
Share

You caught him red-handed! :D

avatar image sandhillceltic · Mar 10, 2013 at 01:41 PM 0
Share

What? How was I caught red handed?

avatar image Khada · Mar 18, 2013 at 03:11 PM 0
Share

Haha, nice.

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

14 People are following this question.

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

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Apply Damage To Player On Collison With Specific Game Object 1 Answer

Problem with PlayerHealth and renderer 1 Answer

Destroy gameobject 2 Answers


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