• 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
4
Question by Justin Warner · Nov 30, 2010 at 07:17 PM · gameobject

Get the gameobject that is connected to the script?

Hey,

So, I'm connecting a script, for applying damage, and the function is on its own script, and I want it to destroy whatever object it is on... (Being a prefab).

So, what I'm wondering is how do you make a line of code that makes the gameobject that the script is connected to to destroy...

Like,

if(health <= 0)
{
//What to put in place of the _'s?
Destroy(________________);
}

Any help would be great, Thanks, Justin W.

Comment
Add comment · Show 1
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 Justin Warner · Nov 30, 2010 at 07:18 PM 0
Share

Also, if their is any like, API on this, can you link/give me a term to search to get to it?

Just for referencing, thanks.

5 Replies

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

Answer by Oninji · Nov 30, 2010 at 07:58 PM

If you want to destroy the immediate gameObject.

It would be

if(health <= 0){
   Destroy(gameObject);
}

But in the case of killing ennemies, you often want to kill the parent of the object and so it's.

if(health <= 0){
   Destroy(transform.root.gameObject);
}

this check the attachments to the transform of the object and go up to the highest one.

PS: The Unity Script reference is very usefull to get to know how the functions are built. http://unity3d.com/support/documentation/ScriptReference/

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

Answer by $$anonymous$$ · Jul 15, 2015 at 05:43 AM

This is much easier than you guys are making it. Just do this:

 if(health <= 0)
 {
     Destroy(gameObject);
 }

Or, if you have anything set to a variable you'd like to destroy, use this:

 if(health <= 0)
 {
     Destroy(anObject.gameObject);
 }

OR, if you are getting the error Cannot delete GameObject to prevent data loss, use this:

 if(health <= 0)
 {
     DestroyImmediate(gameObject);
 }

This is all it takes...

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

Answer by MountDoomTeam · Sep 18, 2012 at 01:19 PM

to reference the current script component's calling object do:

 var obj: GameObject;
 obj= gameObject;
Comment
Add comment · 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
1

Answer by Bob5602 · Nov 30, 2010 at 07:58 PM

Actually, wouldn't it be gameObject? From the Unity reference :

// Kills the game object Destroy (gameObject);

// Removes this script instance from the game object Destroy (this);

// Removes the rigidbody from the game object Destroy (rigidbody);

// Kills the game object in 5 seconds after loading the object Destroy (gameObject, 5);

// When the user presses Ctrl, it will remove the script // named FooScript from the game object function Update () { if (Input.GetButton ("Fire1") && GetComponent (FooScript)) Destroy (GetComponent (FooScript)); }

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

Answer by Jay_Adams · Nov 30, 2010 at 07:29 PM

game.Object

but I've got to type more to post...

edit, my bad, don't know why I put the '.' there.

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

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

1 Person is following this question.

avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

GUI Button scripting 0 Answers

how can I change the gameobject at runtime? 2 Answers

How to convert Unity games to Cardboard games for Android/iOS ? 1 Answer

NPC Class looking for help 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