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?
It is making the error, because BoxCollider is a type of collider, not a reference to the current attached collider.
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
//Start or Awake function
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.
answered Nov 02 '10 at 11:24 PM