C# Question - I Am A Student So Dumb It Down For Me

I am in a 101 level course, so this is beyond my scope. Our instructor has us following along with instructions for a Unity 4 project and the script doesn’t work in Unity 5.

Rigidbody2D.velocity = Vector2.zero;
Rigidbody2D.AddForce(jumpForce);

are the areas not working. It says An Object Reference is required for the non-static field.

  1. Does anyone know what the command should be? I capitalized the Rigidbody2D.velocity which fixed the original error. But I have no idea where to go from here.
  2. Does C# ever eventually make sense? Because it seems incredibly confusing.

Thanks for your help!

Try changing this
Rigidbody2D.velocity = Vector2.zero;

Rigidbody2D.AddForce(jumpForce);

To

gameObject.GetComponent(Rigidbody2D).velocity = Vector2.zero;

gameObject.GetComponent(Rigidbody2D).AddForce(jumpForce);

If I’m not mistaken, Unity 5 removes the shortcuts to rigid bodies.
Instead of

Rigidbody2D.velocity 

It now needs to be

GetComponent<Rigidbody2D>().velocity

Final code:

GetComponent<Rigidbody2D>().velocity = Vector2.zero; GetComponent<Rigidbody2D>().AddForce(jumpForce);

In unity 5 you need to store the rigidbody as a variable
when you typed Rigidbody2D.velocity it sure fixed the first issue which is a missing reference
and the reason for this issue to occur is in the old version of unity we used rigidbody2D.AddForce(jumpForce); directly

but in unity5 you need to store it as a variable and then you need to assign that variable,
In other words you need to tell unity where is that variable’s value

just type this

	public Rigidbody2D myRigidBody2D;
	
	void Start()
	{
		//this code will tell unity to get the rigidbody2d component that is attached to the game object which this script is attached to 
	
		myRigidBody2D =   GetComponent<Rigidbody2D>();
	}
	
	void Update()
	{
		if(Input.GetKeyDown(KeyCode.Space))
		{
			myRigidBody2D.AddForce(1000);
		}
		
	}

You can assign the rigidbody2D in the inspector instead of using GetComponent but i advice you to not
do that unless its a temporary solution because you will run into issues specially when changing
scenes

good luck with your journey 2 years ago i have said the same thing you did
but trust me everything will make sense slowly soon enough :slight_smile: