- Home /
how to change the size of a box collider in c#
im using a box collider but i want to change its size for when a new objects equiped so i was wondering how i can change a box collider size in c#
To declare box collider Follow this : In class -> Box Collider box; In start function box = Getcomponent as BoxCollider; After that change value whenever you want like this box.size= new Vector 3(x,y,z);
Answer by Blazor Ramone · Feb 24, 2012 at 11:46 PM
 BoxCollider b = someGameObject.collider as BoxCollider;
 if(b != null)
 {
     b.size = new Vector(1.0f, 1.0f, 1.0f);
 }
 //something like this should work
technically you don't need gameObject in there, or you may need to get a reference to the collider on another game object... 
thank you ... turns out i was just missing the new bit thats why i was getting errors so thank you very much :D
Finally found answer to my problem thank you :D
Answer by MartinH8921 · Jan 25 at 01:54 AM
In 2022, the code provided by the previous answer is no longer working. The following code worked for me:
     BoxCollider col;
 
     void Start()
     {
         col = GetComponent<BoxCollider>(); 
     }
 
     private void OnMouseDown()
     {  
         if(col != null)
         {
              col.size = new Vector3(.5f, 1f, .5f);
         }
     }
I should also mention that I'm using Unity version 2020.3.16f LTS
Your answer
 
 
             Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Scaling objects in C# 2 Answers
How to make sprites occupy the same screen space on different resolutions ? 1 Answer
Change size of an UI object ( Javascript ( Unityscript) ) 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
               
 
			 
                