Search Unity

Box collider created in script gets wrong position

Discussion in 'Scripting' started by hashashin, May 19, 2014.

  1. hashashin

    hashashin

    Joined:
    Mar 31, 2014
    Posts:
    22
    Hello

    I have an empty game object, and at run time (awake) I create a box collider dynamically:

    Code (csharp):
    1.     var c = transform.gameObject.AddComponent(BoxCollider) as BoxCollider;
    2.     c.center = Vector3.zero;
    3.     c.size = transform.localScale;
    4.     c.isTrigger = true;
    5.  
    But when playing, the box collider is not centered in the empty game object.
    Any hint what is going on?

    Best,
    Alberto
     
  2. hashashin

    hashashin

    Joined:
    Mar 31, 2014
    Posts:
    22
    Always great to submit a question and find the solution right away.
    The collider is automatically scalled with the transform scale.
    So, my c.size should be Vector.one:

    Code (csharp):
    1.  
    2. c.size = Vector.one;
    3.  
    Sorry for the noise. Nevertheless, it might help others.

    Thanks