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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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