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 mah_0003 · Nov 14, 2014 at 01:48 PM · c#destroy

Trouble with destroy function

Hi, I am trying to make a cooperative game about two cubes. I am scripting a button to delete or make a door invisible and passable. This script has worked for pressing the button, but as soon as I add a Destroy(other) command it stops working. This is my code:

 public bool pressed = (false);
 public GameObject other;
 
 void OnTriggerEnter(Collider other)
 {
     if (other.tag == "player2")
     {
         gameObject.SetActive(false);
         Destroy(other);
     }
 }


The boolean is to check if the button is pressed, which is mostly just for me in the editor. The GameObject is a reference to my door, which I assign in the editor. Keep in mind that both the door and button are prefabs, and I am editing the version in the scene, not the one in the folder. Now, before I added the Destroy(other); command, the script worked fine. The button was pressed and no errors occurred. As soon as I added the Destroy(other); command, the script hasn't been working and hasn't changed the colour or pressed or anything. I am coming back from a long break of not programming, so I am a bit sketchy about the trouble.

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 AlwaysSunny · Nov 14, 2014 at 10:15 AM 0
Share

You've got a class-scope GameObject named other and the argument passed to OnTriggerEnter is a collider named other - You should always give your variables unique names. Can't comment on what else might be going on without a better understanding of what you're doing, but that might be the source of your immediate issue.

2 Replies

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

Answer by FairGamesProductions · Nov 14, 2014 at 02:04 PM

Switch between the last 2 lines of code. The script is getting deactivated before it can destroy the "other" gameObject.

If the gameObject is not active, then all it's components (including scripts) are also not active.

Comment
Add comment · Show 4 · 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 Bunny83 · Nov 14, 2014 at 02:21 PM 0
Share

The current execution of a method is never interrupted (with the exception of a coroutine but that's not a normal method. Also it can only be "ter$$anonymous$$ated" when reaching a yield). A common problem (which you might have in $$anonymous$$d) is when you're destroying yourself, the references to your own gameobject and script become "pseudo null" that's why you should put a Destroy that kills yourself as the last statement.

avatar image FairGamesProductions · Nov 14, 2014 at 03:59 PM 0
Share

No.

He wants to destroy the object entering the trigger. BUT he is deactivating the script before it can execute the Destroy function.

Also, mah_003, you should use Destroy like this:

 Destroy(other.gameObject);
avatar image mah_0003 · Nov 14, 2014 at 09:54 PM 0
Share

Thank, you. It turns out that I actually just spelt player2 when it was meant to be Player2, but you were right with the last two lines of code, it did set the script inactive before anything could be destroyed.

avatar image FairGamesProductions · Nov 14, 2014 at 11:09 PM 0
Share

No problem :) Glad I could help.

avatar image
0

Answer by Bunny83 · Nov 14, 2014 at 02:18 PM

Your main problem is probably your "other" variable(s)!!!

You have named one member variable of your script "other" and also the parameter of OnTriggerEnter. Inside OnTriggerEnter when you use "other" you will refer to the collider you hit (which is the collider of the object tagged "player2"). This collider you actually destroy. So you don't destroy the whole gameobject, just the collider. You might wanted to destroy the gameobject referenced by your member variable "other".

A classical example of a bad variable name ;) In case of OnTriggerEnter "other" is a well choosen name since we don't know what it will be. We could collide with anything. "otherCollider" would probably be better but other is ok. However a member variable called "other" is suspicious. There is this script and "another" so naturally i would expect other to be of the same type as this script, why else would you call it other?

If it should reference the door gameobject, call it "door". If the script is a more general purpose script, call it "target".

In case you want to keed your two "other" variables, you can use:

 Destroy(this.other);

which will use the member variable and not the local variable. However, that's not recommended. Keep in mind if you rename the member variable you need to reassign it in the inspector.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Why doesn't Destroy() show any effect on my GameObject? 2 Answers

Destroy script still works on 2 Answers

Destroying an object from its 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