• 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 ricefice2 · Dec 24, 2014 at 05:47 AM · destroyontriggerexit

How do I use Destroy(gameObject) on the instance without destroying itself?

Hey, thanks for the help. I'm using Destroy(gameObject) with a onTriggerExit to get rid of my projectiles that are outside the game area. Problem is that after the first projectile that gets destroyed, I get the error saying that the gameObject im trying to access has been destroyed.

I've tried setting my projectiles to static as well, but not really doing the trick.

Any suggestions on how I could use destroy() more effectively?

CHeers!

Comment
Add comment · Show 5
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 StrikeMasterz · Dec 24, 2014 at 06:02 AM 0
Share

What you essentially want to do is to destroy each projectile specifically if it leaves the game world (onTriggerExit), without destroying ALL the projectiles?

avatar image matthewbauer · Dec 24, 2014 at 07:03 AM 0
Share

Be sure that before you call Destroy(gameObject) you de-reference that projectile. So if that projectile is in a list or being kept track of by the thing that shot it, remove it or set the value to null before destroying.

avatar image Divinyx · Dec 24, 2014 at 07:49 AM 0
Share

I second matthewbauer's comment. Without knowing how your code is implemented you might be still trying to access the "destroyed" projectile somewhere in your code. JA_555's solution should work fine

avatar image StrikeMasterz · Dec 24, 2014 at 08:01 AM 0
Share

Yup. I simply edited it to suit C#, but I am not sure how to do it in bool. Anybody have a clue?

avatar image ricefice2 · Dec 25, 2014 at 10:09 AM 0
Share

Thanks for that Strike$$anonymous$$asterz, I've tried adding a tag "projectiles" to my projectile(@ sorrting layers), then added in the "if" statement to my destroy code in boundary, but i still can't access it once one has been destroyed. Am I adding the tag wrong? or is it something else?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ricefice2 · Dec 25, 2014 at 11:21 AM

i ended up having to create a couple tags and switch to one of them as the projectiles fired, then used the destroy function on the switched tags.

Comment
Add comment · Show 2 · 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 StrikeMasterz · Dec 25, 2014 at 01:23 PM 0
Share

That is really weird you need to do that. It should destroy that instance of the object, not the game object itself. $$anonymous$$ay I ask, are you using Instantiate(gameObject) to make the object appear, or are you doing it another way?

avatar image matthewbauer · Dec 25, 2014 at 02:19 PM 0
Share

To expand on Strike$$anonymous$$asterz comment, it is worth mentioning that if you do, projectilePrefab=Instantiate(projectilePrefab) as gameobject; It will error if you Destroy the object in a script and try to instantiate another one using projectilePrefab.

avatar image
0

Answer by JA_555 · Dec 24, 2014 at 07:41 AM

Maybe something like:

 function OnTriggerExit(other : Collider){
 
  if (other.gameObject.tag == "projectile")
     Destroy(other.gameObject);
     }

Sometimes, the OnTriggerExit function relates to the OnTriggerEnter function because of the parameters that can be used. When it comes to colliders, be aware that your answer may be found in related functions

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 StrikeMasterz · Dec 24, 2014 at 07:47 AM 0
Share

Here is the same script, but in C#

 //When Object collides with an object that has a trigger
 void OnTriggerEnter(Collider other) {
 
 //Check if the tag is named "Projectile"
  if (other.gameObject.tag == "projectile")
  //If so, destroy the object
  Destroy(other.gameObject);
 }

Note, you can change gameObject.tag to another class that holds this information, example being transform.tag

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

6 People are following this question.

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

Related Questions

How to destroy an instantiated prefab object and keep instantiating it 1 Answer

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

OnTriggerEnter2D called twice - doing UFO tutorial 1 Answer

walk through an object once 0 Answers

Destroying an instance of a prefab when exiting an area? 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