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

stuck in error loop when trying to use force Rigidbody2D

Discussion in 'Scripting' started by thomasspore, Aug 19, 2022.

  1. thomasspore

    thomasspore

    Joined:
    Aug 18, 2022
    Posts:
    7
    i am trying to use Rigidbody2D to add jump physics to create my first c# project and the code i am using is
    Rigidbody2D.AddForce(Vector2 force);

    this is what i am using to get the force physics and the rest of my code is this
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. Rigidbody2D.AddForce(Vector2 force);
    5. public class NewBehaviourScript : MonoBehaviour
    6. {
    7.     public Rigidbody2D rb;
    8.     public float jumpAmount = 10;
    9.     void Update()
    10.     {
    11.         if (Input.GetKeyDown(KeyCode.Space))
    12.         {
    13.            rb.AddForce(Vector2.up * jumpAmount, ForceMode2D.Impulse);
    14.         }
    15.     }
    16. }
    any assistance would be amazing, i am on unity version 2021.3.8f1
    thanks in advance
     
  2. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    491
    If you have an error, you really should be telling us what the error says.
    But just from looking at your script, you should get rid of line 4. It is outside your class and I doubt it would be valid is it even was in your class.
     
  3. thomasspore

    thomasspore

    Joined:
    Aug 18, 2022
    Posts:
    7
    right yes i completely forgot to add the error and thank you, the error is
    Assets\NewBehaviourScript.cs(4,30): error CS1003: Syntax error, ',' expected
    but as soon as i add the comma it gives the errors
    Assets\NewBehaviourScript.cs(4,31): error CS0103: The name 'force' does not exist in the current context
    and
    Assets\NewBehaviourScript.cs(4,22): error CS0119: 'Vector2' is a type, which is not valid in the given context
     
  4. thomasspore

    thomasspore

    Joined:
    Aug 18, 2022
    Posts:
    7
    i also removed line 4 and got 4 new error messages these being
    upload_2022-8-19_12-25-2.png
    I apologize for bad image quality
     
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    6,003
    You probably didn't assign your rigid body in the inspector.

    Are you following a tutorial here?
     
  6. thomasspore

    thomasspore

    Joined:
    Aug 18, 2022
    Posts:
    7
    i used a tutorial to get half of the code and a different one for the other and i did set rigidbody2d to the sprite i am coding this to
     
  7. thomasspore

    thomasspore

    Joined:
    Aug 18, 2022
    Posts:
    7
    sorry, i used 1 tutorial but opened it twice to double check, my brain got confused, if it does help you understand my issue a bit better here is the tutorial i used: https://gamedevbeginner.com/how-to-jump-in-unity-with-or-without-physics/
     
  8. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    491
    Not sure why you would be trying to assign a sprite to a Rigidbody2D property in the inspector. Just set the property rb in the inspector to be the Rigidbody2D that is attached to the game object that this script is on.
    Perhaps show a screenshot of the inspector for this game object?