- Home /
An instance of type 'UnityEngine.Component' is required to access non static member
So I am sure its something simple I am doing wrong but I get that error for each one of these lines
var controller : CharacterController = GetComponent(CharacterController);
var fwd : Vector3 = transform.TransformDirection(Vector3.forward);
var dwn : Vector3 = transform.TransformDirection(Vector3.down);
if (!controller.isGrounded && !Physics.Raycast (transform.position, dwn, 1)){
Any clue what I am doing wrong.
edit(moved from "answer")
#pragma strict
public class XControls extends MonoBehaviour{
public class Movement{
private var TiltLimit : float = 0.0;
public var MovementSpeed : float = 0.0;
public var MovementSpeedMax : float = 0.0;
public var MovementSpeedMin : float = 0.0;
public var MovementSpeedMinAngle : float = 4.0;
public var MovementSpeedMaxAngle : float = 30.0;
public var Grounded : boolean = true;
var Groundedish : boolean = false;
var HitEnemy : boolean = false;
private var Direction : boolean = false;
var GroundDistance : float = 0.0;
var Jumping : boolean = false;
public var JumpTime : int = 0;
public function Movement(MS : float, MSM : float, MSm : float, MSmA : float, MSMA : float, HE : boolean){
MovementSpeed = MS;
MovementSpeedMax = MSM;
MovementSpeedMin = MSm;
MovementSpeedMinAngle = MSmA;
MovementSpeedMaxAngle = MSMA;
HitEnemy = HE;
}
function Start ()
{
Screen.orientation = ScreenOrientation.AutoRotation;
}
function Update()
{
var controller : CharacterController = GetComponent(CharacterController);
var fwd : Vector3 = transform.TransformDirection(Vector3.forward);
var dwn : Vector3 = transform.TransformDirection(Vector3.down);
var hit : RaycastHit;
var curSpeed : float = MovementSpeed * Input.GetAxis ("Vertical");
controller.SimpleMove(fwd * curSpeed);
if (!controller.isGrounded && !Physics.Raycast (transform.position, dwn, 1)){
Grounded = false;
}
else if (Physics.Raycast (transform.position, fwd, 1) && !hit.transform.tag == "Enemy"){
HitEnemy = true;
Grounded = false;
}
else{
Grounded = true;
}
if (Screen.orientation != ScreenOrientation.Portrait){
if (Input.gyro.attitude.z > MovementSpeedMinAngle && Input.gyro.attitude.z < MovementSpeedMaxAngle){
Camera.main.transform.rotation.z = Input.gyro.attitude.z;
}
if (Input.gyro.attitude.z >= (MovementSpeedMinAngle + 5)){
if(Direction){
transform.rotation.y = 0;
Direction = false;
}
//add movement
}
if (Input.gyro.attitude.z >= (MovementSpeedMaxAngle - 5)){
if(!Direction){
transform.rotation.y = -180;
Direction = true;
}
//add movement
}
}
}
}
}
Errors:
Assets/Scripts/Java/AI/Mobile_Control.js(40,64): BCE0020: An instance of type 'UnityEngine.Component' is required to access non static member 'GetComponent'.
Assets/Scripts/Java/AI/Mobile_Control.js(41,45): BCE0020: An instance of type 'UnityEngine.Component' is required to access non static member 'transform'.
Assets/Scripts/Java/AI/Mobile_Control.js(42,45): BCE0020: An instance of type 'UnityEngine.Component' is required to access non static member 'transform'.
Assets/Scripts/Java/AI/Mobile_Control.js(47,73): BCE0020: An instance of type 'UnityEngine.Component' is required to access non static member 'transform'.
Assets/Scripts/Java/AI/Mobile_Control.js(50,51): BCE0020: An instance of type 'UnityEngine.Component' is required to access non static member 'transform'.
Assets/Scripts/Java/AI/Mobile_Control.js(65,49): BCE0020: An instance of type 'UnityEngine.Component' is required to access non static member 'transform'.
Assets/Scripts/Java/AI/Mobile_Control.js(73,49): BCE0020: An instance of type 'UnityEngine.Component' is required to access non static member 'transform'.
I've added some to the code since last time but there it is.
Is this code be put in a static function? And would you $$anonymous$$d copying all the error here? I think the full error log will be "...required to access non static member + name of object"
they are all inside. function update()
and here is full error.
An instance of type 'UnityEngine.Component' is required to access non static member 'GetComponent'. for the first one
An instance of type 'UnityEngine.Component' is required to access non static member 'transform'.
for the rest.
Hi, is the class containing this code derived from $$anonymous$$onoBehaviour ? ( = extending $$anonymous$$onoBehaviour in Javascript I think)
yes it is
public class XControls extends $$anonymous$$onoBehaviour{
It's weird. I saw this error before, but the cause is I used "transform" in a static function. Note that "GetComponent" and "transform" need a instance to be called, which means, they can not be put/called in a static function without a instance of a object.
But in your case, if it is just non-static update function, I also don't know why.
Answer by _dns_ · Dec 19, 2014 at 04:16 AM
Your references to Transform component are in the class Movement, not in the class XControls. Movement does not extends Monobehaviors, it's nested inside XControls, that's all, it doesn't inherit anything from XControls either. That's why it won't compile. Also, the Update and Start would not be called.
@Xavier78: In other words delete line 6 and line 79 as well as your $$anonymous$$ovement constructor (line 22 - 29). Since you use your variables directly from your methods you can't simply put those variables in a seperate class.
Your answer
Follow this Question
Related Questions
BCE0019: 'vertices' is not a member of 'Object'. 1 Answer
what's wrong with this?? 2 Answers
Continual position error 0 Answers
An instance of type 'UnityEngine.Animator' is required to access non static member 'GetBool' 1 Answer
UnityEngine.Rect' does not have a visible constructor that matches the.... 1 Answer