Search Unity

Object enter collider trigger destroy Help

Discussion in 'Scripting' started by Luke3671, Oct 13, 2016.

  1. Luke3671

    Luke3671

    Joined:
    Jul 6, 2014
    Posts:
    56
    Afternoon,
    I've been trying to get a script that would Destroy my Jet (Object) when enter the collider/onTrigger. The Jet atm is working on a very basic patrol system using empty objects flying around the map nothing too crazy.

    Basically i've been trying to do a script that when the jet hits the "cube" other side of the map (Where the player can't see no more, It will destroy said object as not needed no more)

    I've been on Unity tutorials & through this might be something that i was trying to do;
    Code below is " https://unity3d.com/learn/tutorials/topics/physics/detecting-collisions-oncollisionenter " just chance name)

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. function OnCollisionEnter (col : Collision)
    4. {
    5.     if(col.gameObject.name == "Jet")
    6.     {
    7.         Destroy(col.gameObject);
    8.     }
    9. }
    But after trying this script the jet still flys into the collision & does his path all over again. (I've also disabled the patrol system i'm using & same result)

    Do i need to add tag to my Jet? or is it missing line of code?

    Thank you
    Luke
     
  2. absolute_disgrace

    absolute_disgrace

    Joined:
    Aug 28, 2016
    Posts:
    253
    Is this script on your collidor itself?

    Have you tried using Set Active to false instead? Something like:
    Code (CSharp):
    1. col.gameObject.SetActive(false);
     
  3. Kalladystine

    Kalladystine

    Joined:
    Jan 12, 2015
    Posts:
    227
    Don't compare with the name unless you really don't have other choice - if that's a Instantiated object from a prefab, it will have " (clone)" added and generally it's the easiest way to things not working. Name is generally only for you, as the designer, so it has a meaning when you look through the inspector and hierarchy.

    You could use a Tag (still a string, but at least it's predefined) using col.gameObject.CompareTag (look it up in the manual, not sure about js/us syntax).

    You could check for a component also - a GetComponent<> used in an if() will return true or false, depending if that component is there. So f.e. check if that object has a YourPatrollingClass component.

    If the location is completely unreachable by the player and it's essentially a "dead zone", don't even check - just destroy on collision (better yet, use a trigger collider with a OnTriggerEnter - it better suits the role as you're not really interested in the collision data and physics interactions, just that it happened).

    Other things to check:
    Add a Debug.Log inside the OnCollisionEnter, but before the if check to see if it's called at all.

    If Destroy doesn't work, this one won't either and it would still keep the object in memory, just disabled - it will also still get some messages (like collisions, since enabling on collision is expected to work).

    PS.
    Check the collision matrix (and the whole page is a good read) - manual. You may have a combination that just doesn't collide.
     
  4. absolute_disgrace

    absolute_disgrace

    Joined:
    Aug 28, 2016
    Posts:
    253
    I didn't even notice the code uses Name and not Tag. Wow. I must be blind!

    OP, use the Tag property instead and make sure your Jet has that tag.