- Home /
Rigidbody.AddForce not being detected
When I am inputting lines to add a force in both 2d and 3d, addforce is said not to exist in the directories and when it comes to running. I keep getting the CS0120 Error. I reinstalled the program with hopes of it being just a glitch in the installation but it still appears to be missing. Edit:
void Update () {
if(Input.GetKey(KeyCode.D)){
Rigidbody2D.AddForce (Vector2.right);
}
}
I just tried it with a basic line added the component to a sprite to see if it would work but for some reason it just doesn't
Show us your code with enough context to see where it is and what you're doing with it. Format all pasted code with the 101010 button.
I have the same problem too. When i write Rigidbody.AddForce (vector3) for exemple, the AddForce is not recognize! Any suggestions?
Answer by tanoshimi · Mar 12, 2015 at 03:19 PM
Rigidbody is the class. You want the instance of the class. And Capitalisation matters: AddForce not addforce.
GetComponent<Rigidbody2D>().AddForce (Vector2.right);
Thanks for the suggestion but it doesn't seem to work either, I get the same error and message. The capitalization is as I've written it so it shouldn't have been a problem either.
Never$$anonymous$$d, I just got your method to work, thanks, it took some fanagling as when I used my original code with a lowercase r for rigid body, unity restructured it since it was thought to be in an older format and it spat out this which worked
void Update () {
if (Input.Get$$anonymous$$ey ($$anonymous$$eyCode.W)) {
GetComponent<Rigidbody2D>().AddForce = Vector2.up * speed;
}
So Thanks For the Help!
Answer by HolBol · Mar 12, 2015 at 02:53 PM
Well addforce won't be found because it's AddForce- case sensitive. Also, you don't seem to be giving a value to the amount. Have you tried
Rigidbody2D.AddForce (Vector2.right * someVar);
?
I type AddForce as given since a lot of the problems with it I saw when researching solutions were because of it. I type it in correctly and I've tried different variables but still nothing.
Your answer
Follow this Question
Related Questions
Why is rigidbody.addForce calling a Rigidbody2D function? 0 Answers
Add force once when object is created 1 Answer
how to make "addForce" only affect one of several connected objects (with hinge points) 0 Answers
InvalidCastException: Cannot convert from source type to destination type using a Rigidbody2D 2 Answers
Unity 2D Physics .AddForce 1 Answer