Search Unity

Changing the size of a box collider at runtime:

Discussion in 'Scripting' started by All_American, Sep 11, 2013.

  1. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    I have a gameobject>prefab and it has a box collider attached to it. On runtime I want to make the box colliders "Y" size bigger.

    How is this possible??
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
  3. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    357
    You can use the collider.size property:

    Code (csharp):
    1. BoxCollider collider;
    2.  
    3. const float ySize = 100.0f;
    4.            
    5. collider.size = new Vector3(collider.size.x, ySize, collider.size.z);
     
  4. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    Yes..that did it grizzy...I thought I would need to do something like "gameobject.collider.size ="
     
  5. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    It broke the gameobject though. once a restart button is touched to clear the list the object doesn't get destroyed...

    This is how I have it set up-

    Code (csharp):
    1.  
    2. /
    3. /
    4. /
    5. void playersDDcard()
    6.     {
    7.         GameObject DDoneCard = playersDD3card();
    8.         // check card is null or not      
    9.         if (DDoneCard == null)
    10.         {
    11.             Debug.Log("Out of Cards");
    12.             return;
    13.         }
    14.         DDoneCard.transform.position = new Vector3(xPosDD, yPosDD, zPosDD);
    15.         DDoneCard.transform.rotation = Quaternion.Euler(0, 90, 0);
    16.         collider.size = new Vector3(collider.size.x, ybcDDcard, collider.size.z);
    17.         hand.Add(DDoneCard);
    18.         cardsDealt++;
    19.     }
    20. /
    21. /
    22. /
    23.  
     
  6. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Clearing a list of GameObjects isn't the same as deleting the GameObjects.

    Are you actually calling Destroy?
     
  7. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    Yes Dan.

    It worked fine before I adjusted the collider .y size.
     
  8. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    un-related.. theres no way a collider size adjustment would cause what youre talking about. It will be something else.
     
  9. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    I commented out the collider code from the above code and when I click to restart the hand that gameobject disappears with everything else like it did before. If I leave that code it doesn't work.
     
  10. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I think you need to use GetComponent from the gameObject to get the instance of the box collider and then change the size.
    Code (csharp):
    1.  collider = DDoneCard.transform.gameObject.GetComponent<BoxCollider>();
    Make sure you create the variable in the loop. It might not destroy it because you have a reference to it.
     
    Last edited: Sep 12, 2013
    SaymenTH14 likes this.
  11. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    I tried that but not that exact way. I will give it a go this evening.

    Thanks.
     
  12. All_American

    All_American

    Joined:
    Oct 14, 2011
    Posts:
    1,528
    That did it.

    Thanks.