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

4 Answers

4

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.

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;

Edited my post.

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.

CSHARP:

			BoxCollider collider = GetComponent<BoxCollider>();

			collider.size;

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.