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 /
This question was closed Sep 18, 2013 at 09:17 AM by Fattie for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Crilled · Aug 10, 2013 at 08:36 PM · gameobjectinstantiatedestroycollide

Why won't this object destroy?

I have the script set up so when my player collides into the coin it should dissapear and be destroyed. (this code is attached to the coin)

     void OnTriggerEnter(Collider myTrigger){
         if (myTrigger.gameObject.name == "Player"){
             Destroy(gameObject);
             PlayerScript.coin_total += 1;
         }
     }


The coin is instantiated after a monster is killed in this code attached to the monster game object. Basically when it gets hit by a bullet he will be destroyed and drop a coin.

 void OnTriggerEnter(Collider myTrigger){
         if (myTrigger.gameObject.name == "Bullet(Clone)"){
             Destroy(gameObject);
             Instantiate(coin, transform.position, transform.rotation);    
         }
     }


The monster gets destroyed and drops the coin just fine, but when the player collides with it, it doesn't disappear or add to his coin count. I found that if a monster is colliding with the player while he's colliding with the coin it works, but I don't know why. I'm assuming it's because I'm initiating the coin from the monster's script. But if that's the case I don't know why my bullets work fine when they're initiated from the Player's script.

Comment
Add comment · Show 2
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 ArkaneX · Aug 10, 2013 at 09:27 PM 0
Share

Have you tried to debug it? Does the OnTriggerEnter from your first sample is even called?

Besides - googling for 'Unity OnTriggerEnter', brought me some interesting entries. I believe that reading first two of them, found in Unity Answers will help a lot.

avatar image Crilled · Aug 10, 2013 at 10:06 PM 0
Share

Solved it......

ok well i'm an idiot I needed to add a rigidbody to my player

I'm dumb my bad.

1 Reply

  • Sort: 
avatar image
0

Answer by YoungDeveloper · Aug 10, 2013 at 08:42 PM

Destroy(myTrigger.gameObject); ?

Comment
Add comment · Show 5 · 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 ArkaneX · Aug 10, 2013 at 09:05 PM 0
Share

According to the op script is attached to the coin, so this would destroy player :)

avatar image YoungDeveloper · Aug 10, 2013 at 09:08 PM 0
Share

Wasn't sure what should destroy what, if so then:

 Destroy(this.gameObject);
avatar image Crilled · Aug 10, 2013 at 09:14 PM 0
Share

Tried this, it does the same thing and only deletes if i'm also colliding with the monster. Otherwise I just run over the coin and nothing happens x.x

avatar image YoungDeveloper · Aug 10, 2013 at 09:22 PM 0
Share

Then ins$$anonymous$$d of checking the collision in each coin, you can set one collision check on player itself.

 void OnControllerColliderHit(ControllerColliderHit hit) {
    if(hit.gameObject.tag == "Coin"){
    Destroy(hit.gameObject);
    PlayerScript.coin_total += 1;
    }
 }

//For this you must add a tag for the coin called "Coin".

By the way, shouldn't it be gameObject.tag, ins$$anonymous$$d of your gameObject.name, because your checking tag.

avatar image Crilled · Aug 10, 2013 at 09:30 PM 0
Share

I was checking the name not the tag, also I just tried your code and it didn't work.

Follow this Question

Answers Answers and Comments

16 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

Related Questions

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

How to insert an object after Destroy(gameObject) 2 Answers

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

Having problem with Destroying GameObject 0 Answers

Destroying 2D Array of Instantated Objects 2 Answers


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