it says an intance of type "unityEngine.rigidbody" is requrid to access non static member addrelativetorque. what does this mean??\

#pragma strict

var rotationSpeed = 100;

function Update ()  
{
     var rotation : float = Input.GetAxis ("Horizontal") * rotationSpeed;
     rotation *= Time.deltaTime;
     Rigidbody.AddRelativeTorque (Vector3.back * rotation) ;

This is actually just a follow up question of [this one][1]. I actually answered that already with my comment over there. [1]: http://answers.unity3d.com/questions/1269403/it-syas-expected-insert-a-semicolan-at-the-end-wer.html

3 Answers

3

It means that you’re trying to refer to the class itself, when you need to refer to an instance of the class, in this case by using GetComponent.

GetComponent(Rigidbody).AddRelativeTorque (Vector3.back * rotation);

Hi @habfans7,
You’re missing the rigidbody declaration in Awake() or Start(), I’ll show you the right code in C#:

public class MyClass : MonoBehaviour
{
    private Rigidbody _rb;

    void Awake()
    {
        _rb = GetComponent<Rigidbody>();
    }
    void Update()
    {
        _rb.AddRelativeTorque(Vector3.back * rotation);
    }
}

Hope this will help.

This is not the "right" code, as you have removed all the OP's code from Update() function

You’re trying to access a non-static function AddRelativeTorque with the Class name, which is wrong. To access non-static members of a class you first need an instance (Object) of it. Therefore, the correct code would be :

 #pragma strict
 
 var rotationSpeed = 100;
 var rotation ;
var rb : Rigidbody;

function Awake(){
rb = GetComponent(Rigidbody);
}

 function Update ()  
 {
      rotation  = Input.GetAxis ("Horizontal") * rotationSpeed * Time.deltaTime;
      rb.AddRelativeTorque (Vector3.back * rotation) ;
}

@habfans7 i don’t know much JS, so the variable declaration could be wrong.
Accept this answer if it solves the problem.

Also not "right" code, with multiple errors. (No type for rotation, which shouldn't be declared as a class variable anyway, not using private for what should be private variables, wrong syntax for GetComponent. Also messed up formatting, which isn't technically an error, but makes things unnecessarily hard to read.) I'm not trying to be mean, but it only takes a minute to test in Unity before posting, and broken code isn't all that helpful for someone who doesn't have much experience with programming.