Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Don't know how to use OnTriggerEnter instead of OnCollisionEnter

Discussion in 'Scripting' started by Haxxy28, Nov 18, 2019.

  1. Haxxy28

    Haxxy28

    Joined:
    Nov 1, 2019
    Posts:
    19
    Just gonna get straight to the point as I assume its a simple answer I just can't work out.
    I have this script which i'm currently using:
    Code (csharp):
    1.  
    2. private void OnCollisionEnter(Collision collisioninfo)
    3.     {
    4.         if (collisioninfo.collider.tag == "Obstacle")
    5.         {
    6.             Destroy(gameObject);
    7.         }
    8.         if(collisioninfo.collider.tag == "Destroyer")
    9.         {
    10.             Destroy(gameObject);
    11.         }
    12.  
    Lets say the Script is ScriptA and the obstacle is testobject1
    ScriptA is on testobject

    The problem I have is that sometimes the destroyer (the object in which the testobject1 touches to cause it to destroy) can get catch on the testobject1 its trying to clear. So I wanted to convert the destroyer to a trigger so that it doesn't have the collider on it, however what I've tried does't work. I need it to be able to destroy a whole variety of objects for example testobject2, testobject3 and testobject4, when it goes in the box collider's area. so a script which could go on the destroyer would be ideal if that is possible, but if it has to be each individual object, that works to. Thank you for any help you can give me, not matter how small. Even if its just a point in the right direction.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
  3. Haxxy28

    Haxxy28

    Joined:
    Nov 1, 2019
    Posts:
    19
    Thanks for trying to, I found this code at the bottom to destroy anything that enters the trigger:
    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4.  
    5. public class Example : MonoBehaviour
    6. {
    7.    // Destroy everything that enters the trigger
    8.    void OnTriggerEnter(Collider other)
    9.    {
    10.        Destroy(other.gameObject);
    11.    }
    12. }
    However nothing happens, can you give me any help?

    EDIT: I Feel really stupid, I forgot to tick the box for the box collider...
    I guess you learn from your mistakes
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    And did you setup everything correctly? There isn't much to go on other then you missed setting something up. Your colliders. One is a trigger. One has a rigidbody. Just make sure things are setup right.