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
1
Question by RuslanGuroSigma · Aug 22, 2013 at 07:02 AM · gameobjectdestroyenemygun

How to destroy GameObject's script(s)?

So I am trying to make so that when I am shooting the enemies, they could stop moving (Deleting the EnemyAI script by killing him) and I tried to do the: Destroy.script(EnemyAI) or Destroy(EnemyAI.js) but nothing is working. Please help!

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 getyour411 · Aug 22, 2013 at 07:15 AM 0
Share

It depends on where this Destroy() is co$$anonymous$$g from, and getting the syntax right. Post how you are defining EnemyAI

Try this

http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by jjkiii · Aug 09, 2014 at 03:20 AM

Mattsson almost completed the question. In C# i would...

 OnTriggerEnter(Collider other)
 {
      //assuming your bullet is tagged as bullet and the enemy has no health.
      if(other.gameObject.tag == "bullet")
      {
           Destroy(GetComponent<EnemyAI>());
      }
 }

Put this on the same object that the EnemyAI script is!

You can use GetComponent() for many things as shown here: http://docs.unity3d.com/ScriptReference/GameObject.GetComponent.html

Hope This Helps! =D

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 rutter · Aug 09, 2014 at 03:12 AM

If you're making the destroy call from the EnemyAI:

 Destroy(this);

If you're doing it from some other object:

 //C# version
 //assumes you have a GameObject variable named "enemy" which is NOT null
 Destroy(enemy.GetComponent<EnemyAI>());

 //JS version
 Destroy(enemy.GetComponent(EnemyAI));

It's impossible to give more detail without knowing more about your scripts. If you can tell us more about your setup, we might be able to give a more specific answer.

In general, though, you should probably look up GetComponent tutorials.

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 mattssonon · Aug 22, 2013 at 07:13 AM

You need to get ahold of the component properly, e.g. by running Destroy(GetComponent(EnemyAI));. But I would suggest you create a static variable, e.g. a bool called canMove, in EnemyAI.js, which controls whether or not the enemy is moving, and then set this variable to false instead of running Destroy().

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 Kiwasi · Aug 09, 2014 at 10:29 AM 0
Share

Bad idea, a static variable will restrict you to only one enemy.

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

20 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

How to Destroy a gameobject on collision 3 Answers

How to delay lines of code. 2 Answers

Destruct gameobject by call from another script 1 Answer

Need help with AI script 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