Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
1
Question by user-4565 (google) · Nov 02, 2010 at 10:49 PM · colliderboxcrouch

Changing Box Collider size

Hey there, im trying to change the BoxCollider size of my player object. It is to use a crouch function, the script is simply "BoxCollider.size = Vector3(1.2,2.5,1.2);" when uncrouched and crouched it is "BoxCollider.size = Vector3(1.2,1.25,1.2);". But the engine keeps saying an instance of type UnityEngine.BoxCollider is required to access "size". I dont know why its saying this, because I have a boxcollider attached to my player.

I also tried using "collider.size" instead of "BoxCollider.size" and then it says something about it needs CharacterController.Height.

Any ideas of why its doing this?

standing code:

function FixedUpdate() {
if (grounded) {
moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
controller = GetComponent(CharacterController);
controller.height = 2.0;
collider.size = Vector3(1.2,2.5,1.2);

crouching code:

else if (Input.GetButton ("left ctrl")) {
moveDirection *= crouchspeed;
controller = GetComponent(CharacterController);
controller.height = 1.0;
collider.size = Vector3(1.2,1.25,1.2);
}

Comment
Add comment
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

4 Replies

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

Answer by Atnas1010 · Nov 02, 2010 at 10:55 PM

Try this

var characterController = GetComponent(CharacterController) as CharacterController;
characterController.height = 2.0;
var boxCollider = GetComponent(BoxCollider) as BoxCollider;
boxCollider.size = Vector3(1.2,2.5,1.2);

It is making the error, because BoxCollider is a type of collider, not a reference to the current attached collider.

Comment
Add comment · Show 5 · 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 user-4565 (google) · Nov 02, 2010 at 10:59 PM 0
Share

it is saying charactercontroller.size is not found. :/ I can change the height of the charactercontroller, but not the collider of it.

avatar image Atnas1010 · Nov 02, 2010 at 11:04 PM 0
Share

You need to explain yourself better. Do you have a charactercontroller or a boxcollider attached? The code I posted works for changing the size of a boxcollider (as you said in your question) What is it you actually want to do?

avatar image Atnas1010 · Nov 02, 2010 at 11:05 PM 0
Share

If it is a charactercontroller, this would be the code collider.height = 1;

avatar image user-4565 (google) · Nov 02, 2010 at 11:06 PM 0
Share

I have both a charactercontroller and a box collider, it is a player im trying to make crouch. I have the code to change the charactercontroller height, but that code alone does not change the collider for the player. I have that exact code in my script, I will update my question to include the whole function.

avatar image Atnas1010 · Nov 02, 2010 at 11:14 PM 0
Share

Edited my post.

avatar image
2

Answer by Proclyon · Nov 02, 2010 at 11:24 PM

Ok so here's your problem.

The compiler says you need an instance of the "Object" you are modifying.

That means you need to GET the object information and put it into a variable. In this case I'm sticking with GameObject or boxCollider or any object in general. You simply ATTEMPT , with huge emphasis on ATTEMPT , to GET the object using the sample Atnas just provided you with in his answer. Check ALL get with an if statement.

I don't know JS (so thank Atnas! :D ) but for C# I can add this-->

//Fields and members

CharacterController someCharacterController;

//Start or Awake function

if (GetComponent<CharacterController>() != null)
{
     someCharacterController = GetComponent<CharacterController>();
}
else
{
     Debug.Log("Character Controller missing in ...this... script");
}

This very same thing can be done for ALL Unity3D objects. Everytime you want to get information out of an object the script will need a place to put that information stored inside an object. The less you do this of course the less work and time for both you and the computer , so try to be smart about it instead of just letting everything know everything ( Like a peer to peer network = slow , so is bad Object Orintation ) ) Once you have the object instance you can acces all it's public variables (for more on that just look up inheritance or acces modifiers which are search phrases that can help you find infinite resources on that which can explain it MUCH better than I ever will) )

The if ( not null ) code is a great way to never get lost in the maze of crashes and bugs, since if it fails, your console will simply tell you what went wrong and WHERE it went wrong aswell saving loads of time , and keeping your code clean.

Comment
Add comment · 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
1

Answer by Stitchey_ · Feb 18, 2015 at 10:53 AM

CSHARP:

             BoxCollider collider = GetComponent<BoxCollider>();
 
             collider.size;

Comment
Add comment · 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
0

Answer by ramp · Sep 03, 2013 at 06:27 AM

var box_collider : BoxCollider;

box_collider.size.x =10; box_collider.size.y=10; box_collider.size.z =10;

try this.take the reference from another GameObject in which BoxCollider already attached.

Comment
Add comment · 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

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

2 People are following this question.

avatar image avatar image

Related Questions

Internal collisions 1 Answer

Is it possible to skew a box collider? 1 Answer

3D text with box collider Android 1 Answer

how to resize box collider? 4 Answers

Keeping speed while crouching in midair and remove clipping while returning to standing 2 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