Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Xavier78 · Dec 18, 2014 at 11:34 PM · javascripterrorcomponentbce0020

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.

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Trungdv · Dec 19, 2014 at 12:27 AM 0
Share

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"

avatar image Xavier78 · Dec 19, 2014 at 12:36 AM 0
Share

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.

avatar image _dns_ · Dec 19, 2014 at 01:04 AM 0
Share

Hi, is the class containing this code derived from $$anonymous$$onoBehaviour ? ( = extending $$anonymous$$onoBehaviour in Javascript I think)

avatar image Xavier78 · Dec 19, 2014 at 01:07 AM 0
Share

yes it is

public class XControls extends $$anonymous$$onoBehaviour{

avatar image Trungdv · Dec 19, 2014 at 01:10 AM 0
Share

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.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Dec 19, 2014 at 04:36 AM 0
Share

@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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

30 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges