Problem with velocity

i cannot resolve this problem ‘ball’ does not contain a definition for ‘Rigidbody2D’ and no extension method ‘Rigidbody2D’ accepting a first argument of type ‘ball’ could be found (are you missing a using directive or an assembly reference?) (CS1061) (Assembly-CSharp)

In your code you have rigidbody2D instead of Rigidbody2D. Case-sensitivity is the issue here.

On a related note, you will see that you have a mixture of capitalisation in your scripts/classes, ball / paddle and LevelManager for example, it would serve you well to capitalise the script names and class names, along with method names. This will make you code a lot more readable and less confusing as you will be able to more easily differentiate between types and variables.

Line 5 for example;

public paddle paddle;

becomes;

public Paddle paddle;

I hope this is useful.

I made the changes you suggested but it still says the same thing have you anymore suggestions?

I’m guessing you are not using the older version of Unity are you? As I notice the warning/error at the top of Mono which states about Application.LoadLevel() being obsolete.

If you are using Unity5 you will need to change how you access the components on a GameObject.

Instead of using;

this.Rigidbody2D.velocity

use

gameObject.GetComponent<Rigidbody2D>().velocity

Note: Due to every GameObject having a Transform in Unity, at this time you can still use;

gameObject.transform

instead of using;

gameObject.GetComponent<Transform>()

but for all other components, use the GetComponent() method.

If you paste that whole code block up I will happily to a quick convert of it for you and paste it back up…


See also;

1 Like
public class Ball : MonoBehaviour {
	public Paddle paddle;

	private bool hasStarted=false;
	private Vector3 paddleToBallVector;

	// Use this for initialization
	void Start () {
		paddleToBallVector=this.transform.position-paddle.transform.position;
	
	}
	
	// Update is called once per frame
	void Update ()
	{
		if (!hasStarted) {
			this.transform.position = paddle.transform.position + paddleToBallVector;

			if (Input.GetMouseButtonDown (0)) {
			hasStarted=true;
				gameObject.GetComponent<Rigidbody2D>().velocity;
			}
		}
	
	}
}

You still need to add the value, sorry, I was only giving you half of the line to show you the difference with the GetComponent() method.

So,

gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(2f, 10f);

Another area people sometimes come unstuck is the class names and script names, in Unity they have to be the same, so, now that you have changed the case of the classes Ball and Paddle, but sure that the script names match also.

I thought that but i was not sure , Thanks a million for all the help

1 Like

You’re more than welcome Paul, anything else just post up :slight_smile:

1 Like

useful explanations, thanks Rob.

1 Like

You are very welcome @Engin :slight_smile:

Hey Rob :slight_smile:
Does velocity always need to be a float. I looked a bit on the unity page and as far as I understood it does not. If that should be the case why are we using floats?

Hello Bryan,

velocity on a Rigidbody is represented by a Vector3.

The constructor for a Vector3 receives floats which represent the x, y and z.

The same is true for a Rigidbody2D, the difference being velocity is represented by a Vector2, the constructor for which again receives floats.

Hope this helps. :slight_smile:


See also;

Great! Thanks Rob

1 Like

You’re more than welcome :slight_smile:

1 Like