• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by quantumarchi · Feb 24, 2012 at 10:09 PM · c#sizeresizebox collider

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#

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ajit_singh · Jan 31, 2021 at 12:55 PM 0
Share

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);

2 Replies

· Add your reply
  • Sort: 
avatar image
8
Best Answer

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
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Blazor Ramone · Feb 24, 2012 at 11:53 PM 1
Share

technically you don't need gameObject in there, or you may need to get a reference to the collider on another game object...

avatar image quantumarchi · Feb 25, 2012 at 01:17 AM 0
Share

thank you ... turns out i was just missing the new bit thats why i was getting errors so thank you very much :D

avatar image Teddypwnage · Jul 13, 2013 at 01:08 PM 0
Share

this worked great! thank you

avatar image MrMoonWalkingBear · Nov 25, 2014 at 07:27 PM 0
Share

Finally found answer to my problem thank you :D

avatar image
0

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);
         }
     }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MartinH8921 · Jan 25 at 02:07 AM 0
Share

I should also mention that I'm using Unity version 2020.3.16f LTS

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges