x


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

asked Nov 02 '10 at 10:49 PM

user-4565 (google) gravatar image

user-4565 (google)
23 9 10 18

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Nov 02 '10 at 10:55 PM

Atnas1010 gravatar image

Atnas1010
1.1k 6 10 26

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

Nov 02 '10 at 10:59 PM user-4565 (google)

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?

Nov 02 '10 at 11:04 PM Atnas1010

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

Nov 02 '10 at 11:05 PM Atnas1010

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.

Nov 02 '10 at 11:06 PM user-4565 (google)

Edited my post.

Nov 02 '10 at 11:14 PM Atnas1010
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Nov 02 '10 at 11:24 PM

Proclyon gravatar image

Proclyon
1.4k 10 13 32

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1687
x161
x40

asked: Nov 02 '10 at 10:49 PM

Seen: 9215 times

Last Updated: Nov 02 '10 at 11:08 PM