- Home /
An instance of type 'UnityEngine.BoxCollider2D' is...
Hi, I am getting this error code on my new js script - Assets/Scripts/MovementScript.js(80,88): BCE0020: An instance of type 'UnityEngine.BoxCollider2D' is required to access non static member 'size'.
First of all, i am not sure what this means Secondly it says the error is on this line - if(vm <= 0 && contact.point.y <= transform.position.y - (BoxCollider2D.size.y/2) && Vector2.Angle(Vector2.up, contact.normal) <= slopeLimit){
I have checked through everything but i am still not sure what the problem is.
Here is my entire code incase you need it -
#pragma strict
var moveSpeed : float = 8;
var jumpSpeed : float = 25;
var fallSpeed : float = 64;
var maxFallSpeed : float = 60;
var maxUpSpeed : float = 25;
var slopeLimit : float = 45;
var dashSpeed : float = 120;
var fly : boolean;
private var vm : float;
var onGround : boolean;
private var xScale : float;
private var h : float;
private var v : float;
private var jump : boolean;
private var anim : Animator;
private var boxcoll : BoxCollider2D; //If boxCollider2D is used replace w/ ... private var boxColl : BoxCollider2D;
function Start () {
xScale = transform.localScale.x; //Get correct starting Orientation for player.
anim = gameObject.GetComponent("Animator"); //Get Animations for character
boxcoll = GetComponent(BoxCollider2D); //if boxCollider2D is used replace w/... boxColl = GetComponent(BoxCollider2D);
}
function Update () {
h = Input.GetAxisRaw("Horizontal"); //get horizontal input
v = Input.GetAxisRaw("Vertical"); //get vertical input
jump = Input.GetKey(KeyCode.Space); //get jump input
}
function FixedUpdate(){
if(onGround && jump){ //Are we grounded can we jump?
vm = jumpSpeed;
}
var moveH : float = h * moveSpeed * Time.deltaTime; //Smooth horizontal movement
//Flip character orientation
if(h < 0){
transform.localScale.x = -xScale;
} else if(h > 0){
transform.localScale.x = xScale;
}
anim.SetFloat("VM", vm); //Animate VM parameter
anim.SetBool("Grounded", onGround); //Animate Grounded parameter
rigidbody2D.AddForce(Vector2.right * moveH); //Apply horizontal movement
rigidbody2D.AddForce(Vector2.up * vm); //Apply vertical movement
if(fly){ //Can we fly?
if(v > 0){
//Fly like a bird!!!
vm += v * Time.deltaTime * 20;
} else {
//Uh oh it's gravity
vm -= fallSpeed * Time.deltaTime;
}
} else {
//Gravity ... ouch!
vm -= fallSpeed * Time.deltaTime;
}
vm = Mathf.Clamp(vm, -maxFallSpeed, maxUpSpeed); //Clamp vertical speeds
rigidbody2D.velocity = Vector2.zero; //Stop movement if there is no input
onGround = false; //Clear out grounding check
}
function OnCollisionEnter2D(c : Collision2D){
CheckCollision(c);
}
function OnCollisionStay2D(c : Collision2D){
CheckCollision(c);
}
function CheckCollision(c : Collision2D){
for(var contact in c.contacts){
//Check for floor hit
//If boxCollider2D is used replace circCollider.radius w/ (boxCollider2D.size.y/2)
if(vm <= 0 && contact.point.y <= transform.position.y - (BoxCollider2D.size.y/2) && Vector2.Angle(Vector2.up, contact.normal) <= slopeLimit){
onGround = true;
vm = Mathf.Max(0, vm);
}
}
}
Please ask if you need any more information.
Does the object you have this script on have a box collider? If not attach one by selecting it in the editor, go Components->Physics2D->Box Collider
Answer by robertbu · Feb 20, 2014 at 04:10 PM
You are close. You need to use the 'boxcoll' variable you initialized in Start() to access 'size':
if(vm <= 0 && contact.point.y <= transform.position.y - (boxcoll.size.y/2) && Vector2.Angle(Vector2.up, contact.normal) <= slopeLimit){
Thanks, This stopped but now i have another problem, i have applied the script and now when i test the game the characters 3 frame animation plays but he doesn't move in and direction, do you know how i can fix this?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
GuiTexture Width Change 1 Answer
Audio not playing [Fixed] 1 Answer
Why will this timer not count down? 1 Answer
Adding Force to a rolling ball 1 Answer