- Home /
Object Reference Required to access non-static member 'UnityEngine.RigidBody.AddRelativeTorque'
I am attempting to create a rotation with rigidbody's add relative torque function. I am getting the error: An Object Reference Required to access non-static member 'UnityEngine.RigidBody.AddRelativeTorque' and I don't know how to fix this. This is my code:
public class NewRotation : MonoBehaviour {
void FixedUpdate() {
if (Input.GetButtonDown("Horizontal(-)")) {
Rigidbody.AddRelativeTorque(0, 0, -15);
}
if (Input.GetButtonDown("Horizontal(+)")) {
Rigidbody.AddRelativeTorque(0, 0, 15);
}
}
}
Answer by robertbu · Mar 28, 2014 at 08:59 PM
You want 'rigidbody' with a lower case 'r'. 'Rigidbody' is the class. Example:
rigidbody.AddRelativeTorque(0, 0, -15);
Thanks that removes the errors but now when I run it in unity, the object that I've set it to won't rotate. In fact nothing will rotate have you got any ideas? (Yes, I have put the rigid body component on the object)
$$anonymous$$y guess is that it has something to do with how you've setup your buttons. I modified your code to use Get$$anonymous$$eyDown() and tested it, and it worked.
using UnityEngine;
using System.Collections;
public class Bug25a : $$anonymous$$onoBehaviour {
void FixedUpdate() {
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.RightArrow)) {
rigidbody.AddRelativeTorque(0, 0, -15);
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.LeftArrow)) {
rigidbody.AddRelativeTorque(0, 0, 15);
}
}
}
Thanks it's still not working in my unity but I'll send it to my friend and have him test it. Thanks for all the help.
Do this:
Start a new scene
Insert cube
Add a Rigidbody component
Turn off gravity
Add my modified version of your script
Play