Script doesnt like rigidbody2D.velocity

Hi there,

Great course and im having fun fo far!

I dont know if this is because I am using unity 5 (although everything else I have googled when there are differences between unity 4 and 5 has come up with an answer) but anyway… I am at the part where I am trying to tweak the Vector on the OnCollisionEnter2D function and ‘rigidbody2D.velocity’ (not typing a string there!) comes with a red underline.

In Unity I get the error: Assets/Scripts/Ball.cs(41,25): error CS1061: Type UnityEngine.Component' does not contain a definition forvelocity’ and no extension method velocity' of typeUnityEngine.Component’ could be found. Are you missing an assembly reference?

Isn’t calling rigidbody like this incorrect anyway?

A lot of stuff I have seen when lookng online is that it is defines before it is called like this…an example definition would be like this:

RigidBody2D rb2d = GetComponent();

but I am not seeing anything like that so far in the script that we have been making that already exists? how does it just know what ‘rigidbody2D’ is?

My apologies if I am unclear…I’m not great at explaining these types of problems online haha…thanks for any help!

Matt

The unity 5 update made rigidbody2D obsolete, you should be using GetComponent(). instead now.

The unityengine namespace (you call it when you say using UnityEngine on the top of the script) already contain some components such as this one, when you say: Rigidbody2D rb2d = GetComponent<Rigidbody2D>(), you are initializing an variable of type Rigidbody2D with the name rb2d, the compiler knows what is an Rigidbody2D type since you added the unityengine namespace, he just don’t know which value you want to address to it (in this case, is the GetComponent<Rigidbody2D>() which is the rigidbody within the gameobject that contains this script)

1 Like

Privacy & Terms