• 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 /
  • Help Room /
avatar image
0
Question by Hodoer · Mar 31, 2020 at 04:20 AM · destroygameobject

Issue with Destroying Gameobjects

Hi, I need some guidance from the community. I've made a scenario where an NPC will spawn from the spawn point and move towards the exit with the help of NavMesh. I got the spawning and the path-finding to work however I'm unable to destroy the game object when it reaches the exit. A screenshot is as attached: alt text

Here are the codes I've used in this scene: Spawning Script

 public class NPC_Spawner : MonoBehaviour
 {
     public GameObject npc;
     public GameObject exit;
 
     void Start ()
     {
         Invoke("SpawnNPC", 2);
     }
 
     public void SpawnNPC ()
     {
         GameObject na = (GameObject) Instantiate(npc, this.transform.position, Quaternion.identity);
         na.GetComponent<NPC_Behaviour>().exit = exit.transform;
         // if (na.transform.position == exit.transform.position)
         // {
         //     Destroy(na);
         //     Debug.Log("Collision Detected!!!");
         // }
         Invoke("SpawnNPC", Random.Range(2,5));
     }
 }

NPC Behaviour Script

 using UnityEngine;
 using UnityEngine.AI;
 
 
 public class NPC_Behaviour : MonoBehaviour
 {
     public Transform exit;
 
     void Start()
     {
         UnityEngine.AI.NavMeshAgent agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
         agent.destination = exit.position;
     }
 }

Exit Script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class NPC_Exit : MonoBehaviour
 {
     void OnTriggerEnter(Collider col)
     {
         if (col.gameObject.tag == "NPC")
         {
             Debug.Log("Collision Detected");
             Destroy(col.gameObject);
         }        
     }
 }
 

The box collider for exit is set 'Is Trigger'. Please let me know what I did wrong here. Your help is much appreciated.

001.jpg (242.3 kB)
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

1 Reply

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

Answer by The_Three_Vs · Mar 31, 2020 at 03:56 PM

Do either your exit or your NPC object have a Rigidbody? In order for OnTriggerEnter to be called, at least one of the colliding objects must have a Rigidbody. If this is the case, I would recommend putting a Rigidbody on the exit and unchecking gravity.

Hope this helps!

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 Hodoer · Apr 01, 2020 at 01:52 AM 0
Share

Thank you very much, this works for me. One question though if it's not too troublesome, what's functionally different between onTriggerEnter and onCollisionEnter? Implementing a Rigidbody works for onTriggerEnter but does not work for onCollisionEnter.

avatar image The_Three_Vs Hodoer · Apr 01, 2020 at 02:04 AM 0
Share

When isTrigger is on, the collider will not interact with other colliders, so it's effectively intangible. Because of this, "collisions" do not happen and OnCollisionEnter isn't called, but objects can still enter the trigger area and activate OnTriggerEnter. Furthermore, while OnTriggerEnter passes in a collider, ex. the other object's BoxCollider, OnCollisionEnter passes in a Collision, which contains additional information about the collision like contact points and impact velocity.

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

198 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 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 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 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 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 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 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 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 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

Unity destroy works at random ? 1 Answer

How to delete tiles when ran over to avoid lag? 0 Answers

c# match 3 destroy... 1 Answer

[2D] Freezing all constraints also destroys gameobject 0 Answers

How to make it so when i shoot a zombie the has been spawned, it will destroy it? 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