Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Can't use Addforce to Rigidbody

Discussion in 'Scripting' started by HWTyrael, Jan 5, 2015.

  1. HWTyrael

    HWTyrael

    Joined:
    Jan 5, 2015
    Posts:
    3
    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.
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Barachiel likes this.
  3. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    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.
     
  4. HWTyrael

    HWTyrael

    Joined:
    Jan 5, 2015
    Posts:
    3
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerController : MonoBehaviour
    5. {
    6.     void FixedUpdate()
    7.     {
    8.         float moveHorizontal = Input.GetAxis ("Horizontal");
    9.         float moveVertical = Input.GetAxis ("Vertical");
    10.  
    11.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    12.  
    13.         Rigidbody.Addforce (movement);
    14.     {
    15. }
    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.
     
  5. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    lowercase rigidbidy
     
  6. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    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).
     
  7. HWTyrael

    HWTyrael

    Joined:
    Jan 5, 2015
    Posts:
    3
    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. :)

    Thank you very much for the fast respones.
     
  8. Barachiel

    Barachiel

    Joined:
    Nov 25, 2012
    Posts:
    147
    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.
     
    HWTyrael likes this.
  9. HelloMeow

    HelloMeow

    Joined:
    May 11, 2014
    Posts:
    279
    "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.
     
  10. saddam751

    saddam751

    Joined:
    Nov 6, 2013
    Posts:
    41
    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.
     
  11. Scorael

    Scorael

    Joined:
    Apr 12, 2015
    Posts:
    12
    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?
     
  12. HelloMeow

    HelloMeow

    Joined:
    May 11, 2014
    Posts:
    279
    Are you using Unity 5? Unity 5 doesn't have shortcut properties anymore. Now you have to use GetComponent.
    Code (csharp):
    1.  
    2. RigidBody rigidBody = GetComponent<RigidBody>();
    3.  
     
    Scorael likes this.
  13. Scorael

    Scorael

    Joined:
    Apr 12, 2015
    Posts:
    12
    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.