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. Dismiss Notice

OnCollision stuff, which one to use?

Discussion in 'Scripting' started by MG, Mar 14, 2015.

  1. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    Hello, i have a Object A, which is moving, when Object A entering Object B, i want object B to be destroyed.

    And the script have to be attacthed to object B

    What do i do?
     
  2. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,663
    You'll probably want to use OnTriggerEnter () and set object B up with a rigid body that is kinematic. Is object B moving/a character? If so then OnCollisionEnter () makes sense, you just check against the tag or name of object B to be sure you don't destroy everything you touch!
     
  3. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    keep in mind if you are using rigidbody2d and 2d colliders you have to use

    working with 2d colliders as trigger
    Code (CSharp):
    1. OnTriggerEnter2D(Collider2D other) {}
    2.  
    3. OnTriggerStay2D(Collider2D other) {}
    4.  
    5. OnTriggerExit2D(Collider2D other) {}
    working with 2d colliders (no trigger set)
    Code (CSharp):
    1. OnCollisionEnter2D(Collision2D other) {}
    2.  
    3. OnCollisionStay2D(Collision2D other) {}
    4.  
    5. OnCollisionExit2D(Collision2D other) {}
     
  4. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    It's seems that I didn't do a good job with explain

    I have 2 objects
    3D Object A: Which is moving
    3D Object B: Which is standing still, in mid air

    The function im looking for: When Object A has entered Object B, i want Object B to be destroyed, and the script shall be attached on Object B

    The current code for Object B, which is not working


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4.  
    5. public class delivery : MonoBehaviour {
    6.     static int i = 0;
    7.     // Use this for initialization
    8.     void Start () {
    9.    
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.  
    15.     }
    16.  
    17.  
    18.  
    19.     void OnTriggerEnter(Collider col)
    20.     {
    21.         if (col.gameObject.tag == "Player")
    22.         {
    23.             Destroy(gameObject);
    24.             carSettings.pizzaAmount--;
    25.  
    26.         }
    27.     }
    28. }
    29.  
    The funny thing about this code, is if i attach the script on Object A, and check the tag, it works by deleting the player
     
  5. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    show us inspector screenshot of object b.

    do u use a rigidbody on it? kinematic? has it a collider (trigger)?
     
  6. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    Here is the insceptors of the objects
     

    Attached Files:

  7. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    object a has no collider, add one... this should help.

    collisions can only be recognized if there are to collider hitting each other
     
  8. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    There is tobad, im using the car from Unity3d version 5, so there should be colliders on the car
     
  9. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    you have to look up in which child gameobject the colliders are and set theire tag to "Player"

    or you have to change your OnTriggerEnter if(cold.gameObject.tag == "..." ) condition
     
  10. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    I just narrowed it down, with which one is "touching" and i set it to Player tag, but didn't help
     
  11. tobad

    tobad

    Joined:
    Mar 12, 2015
    Posts:
    90
    Code (CSharp):
    1.     using UnityEngine;
    2.     using System.Collections;
    3.    
    4.    
    5.     public class delivery : MonoBehaviour {
    6.         static int i = 0;
    7.         // Use this for initialization
    8.         void Start () {
    9.      
    10.         }
    11.      
    12.         // Update is called once per frame
    13.         void Update () {
    14.    
    15.         }
    16.    
    17.    
    18.    
    19.         void OnTriggerEnter(Collider col)
    20.         {
    21.             Debug.Log(col.gameObject.name);
    22.         }
    23.     }
    24.    
    25.  
    use this and look at your console
     
  12. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    theres is nothing in the Log, when i run the code
     
  13. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Make sure the script is attached to the object?
    Maybe you've removed it accidently or intentionally, but on your screenshot there isn't any script attached to Object B.
     
  14. MG

    MG

    Joined:
    Nov 10, 2012
    Posts:
    190
    Suddoha, im not sure, if I didn't apply the script on.

    So i deleted the Object B, and made a new one, a now its working with Toboad script
     

    Attached Files:

  15. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    Great that it works now.
    Then you simply forgot to attach it before. Scripts that you attach to a gameobject always appear as a component in the inspector when you select the gameobject. In your case it's the delivery script.