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

Collider still be there after destroy in Unity5, why?

Discussion in 'Physics' started by Magison, Apr 10, 2015.

  1. Magison

    Magison

    Joined:
    Mar 1, 2013
    Posts:
    21
    Hello,
    I want to check, if two cylindrical gameObjects intersects.
    In Unity 4.x it works fine with the following:
    Code (csharp):
    1.  
    2.  
    3.      // Create new Cylinder GameObject to hold the Collider:
    4. GameObject trigger = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
    5. Destroy(trigger.GetComponent<Renderer>());
    6. Destroy(trigger.GetComponent<Collider>());
    7.  
    8.      // add a mesh collider as convex collider to get collisions by other mesh colliders:
    9. MeshCollider mc = (MeshCollider) trigger.AddComponent(typeof(MeshCollider));
    10. mc.convex = true;
    11. mc.isTrigger = true;
    12.  
    13.       // set the new trigger gameObject as child of this gameObject,
    14. trigger.transform.parent = transform;
    15.    // dummy code to resize. My originalcode is different but ok
    16. trigger.transform.position = transform.position;
    17. trigger.transform.localRotation = Quaternion.identity;
    18. trigger.transform.localScale = Vector3.one;
    19.  
    20.      // add Ridigbody to get Colliders working
    21. Rigidbody rb = (Rigidbody)trigger.AddComponent(typeof(Rigidbody));
    22. rb.useGravity = false;
    23. rb.isKinematic = true;
    24.  
    25.      // add TriggerReturnScript script that do OnTriggerEnter and SendMessage back to this GO
    26. trigger.AddComponent(typeof(TriggerReturnScript));
    27.  
    This solution works very well. But not with Unity5 anymore.
    A cylindrical MeshCollider is created like expected, in the size of my Objects. That works.
    But they get collision like they have attached a Capsule Collider. Half a sphere on each end is looking out of the cylinder.
    GetType(collider) also returns a capsule collider, but i destroyed them.
    In scene view you see only the new created meshcollider in light green lines. You not see the capsule collider. I destroy it at top of the code, but it is still there for the physics.
    In the Inspector there is also only the new meshcollider.
    If i change the code, that i only set the collider (generated with the primitive cylinder) enabled=false, everything is ok.
    But then i have two colliders attached, one mesh active and the capsule disabled.
    Why is the collider still there after destroy in Unity5?
     
    Last edited: Apr 10, 2015
  2. enishii

    enishii

    Joined:
    May 9, 2014
    Posts:
    11
    Didnt tryed it(and i cant right now..), but try using CapsuleCollider instead of the base class
    Destroy(trigger.GetComponent<CapsuleCollider>();
    Maybe im writing something stupid, never needed something like this
     
  3. Magison

    Magison

    Joined:
    Mar 1, 2013
    Posts:
    21
    Thank you. Good idea.
    Testet it. But no. Same Problem with this.