- Home /
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);
}
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.
it is saying charactercontroller.size is not found. :/ I can change the height of the charactercontroller, but not the collider of it.
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?
If it is a charactercontroller, this would be the code collider.height = 1;
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.
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.
Answer by Stitchey_ · Feb 18, 2015 at 10:53 AM
CSHARP:
BoxCollider collider = GetComponent<BoxCollider>();
collider.size;
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.
Your answer
Follow this Question
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