Can't use Addforce to Rigidbody

Hello everyone.

I’m fairly new to Unity and I can’t get around a problem.

When scripting simple move commands I can’t use addforce to rigidbody

Under Void Fixedupdate

Rigidbody.(here should the addforce be added but it tells me that command is not possible and it give a bug error.).

I’m doing this wile viewing the tutorial on “Moving the Player” and I’ve done everything as instructed but it just doen’t want to work.

I’m using the free version of Unity, just in case that could be the problem.

Thanks in advance.

Welcome to the forum!

You should post your script, such that we can see what is going on. The line number at which the error occurs is also important. Don’t forget to use code tags:

1 Like

Make sure you’ve got the upper and lower cases in the right locations. It should be rigidbody.AddForce, just in case that’s the issue.

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour
{
    void FixedUpdate()
    {
        float moveHorizontal = Input.GetAxis ("Horizontal");
        float moveVertical = Input.GetAxis ("Vertical");

        Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);

        Rigidbody.Addforce (movement);
    {
}

It’s not about the upper or lower cases, as I tried with “Float”, “GetAxis”, “Void” to write them in both ways and it worked.

The problem is at the last line in the code. I write “rigidbody.” and then after i write only “A” it says there is no match.

lowercase rigidbidy

I just tried your script and changed the Rigidbody.Addforce to rigidbody.AddForce and, provided you fix the second last bracket there, it works fine (including code completion).

Thank you very much. The thing is that when i write it it autowrites with a “R” but now I made it small and it’s ok. :slight_smile:

Thank you very much for the fast respones.

No worries. =)
The capitalized Rigidbody is it’s own thing and is used differently, but it seems to be what appears first. That’s why you’ll still get autocompletions for it, just not the ones you were after.

1 Like

“Rigidbody” is the class name, whereas in this case, “rigidbody” is the name of a specific instance of that class. By using the class name you can access it’s static members, but not a non-static member of any instance of that class.

Actually its very easy task.
Here is the how to add force to a gameobject with rigidbody

gameObject.rigidbody.AddForce(Vector3.left *12);

We can also use it in if condition-

if(Input.GetMouseButtonDown(0))
{
gameObject.rigidbody.AddForce(Vector3.left *12);
}

By this on pressing left mouse button gameobject will move to slightly left.

I am having the same issue. Addforce does not appear in the list of completions despite me changing “rigidbody” to lower case.

Using the free version and following the tutorial at the moment. Could this be the issue?

Are you using Unity 5? Unity 5 doesn’t have shortcut properties anymore. Now you have to use GetComponent.

RigidBody rigidBody = GetComponent<RigidBody>();
1 Like

Thanks,
That explains a lot. Hope they update their tutorials.
How do I use GetComponent to obtain Addforce?
I am very new to Csharp. Hope you don’t mind helping me out.