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 thooom · May 08, 2014 at 08:13 PM · instantiatedestroytower defense

Have gameObjects destroy themselves from external script

I am making a game that will randomly spawn characters (like in a tower defense game) that will go through a path and then either die or reach a given finish. Once a character is instantiated I add a script as a Component to that character that works as a controller for that specific character. The idea is that the characters are given a random route, all of which have their own specific instructions regarding how to maneuver through the course. When the unit is either dead or reaches the finish I want it to Destroy itself. Is there any smart way I can either pass the gameObject as a variable to the controller script so I can use Destroy(gameObject) or any other way to easily delete the units that are supposed to be removed?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Jeff-Kesselman · May 08, 2014 at 08:15 PM

In any monobehaviour, or sub-class thereof, the variable gameObject is a reference to the owning object.

See https://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.html for everything available within a component.

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
0

Answer by robinrei · May 08, 2014 at 10:18 PM

You could simply do the following in the controller script that you are attaching to the characters:

 if(_hasReachedGoal || _hasDied)
 {
     GameObject.Destroy(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
0

Answer by thooom · May 18, 2014 at 07:08 PM

I solved this by creating a function like this.

function destroyCar(carToDestroy : GameObject) { while(carToDestroy.transform.position.y > -5 && carToDestroy.transform.position.y < 5.5 && carToDestroy.transform.position.x < 10 && carToDestroy.transform.position.x > -10)

{ yield WaitForSeconds(0.5); }
Destroy(carToDestroy); }

I then run this script right after I instantiate an object while I have it accessible by running

destroyCar(carName);

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
0

Answer by screenname_taken · May 18, 2014 at 06:53 PM

Why not a trigger box at the end with the Destroy(...) instruction if the object that enters said trigger box matches with your enemy tag?

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.

Follow this Question

Answers Answers and Comments

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

Related Questions

Instantiating Objects 1 Answer

Can't seem to destroy instantiated objects. 1 Answer

Can a Network.Instantiate()'d object be Object.Destroy()'d? 0 Answers

Checking Instantiate/Destroy has been called | Checking number of scene GameObjects 1 Answer

Destroy last placed object and put a new one? 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