Certain Classes Crossed Out?

I was following a tutorial and the guy in the video wrote the code,
rigidbody2D.angularVelocity = 100f.
but when I write it 3 weird things happen…
for one, there seems to be rigidbody2D and Rigidbody2D.
two, the rigidbody2D is crossed out with alot of other classes.
three, angularVelocity isn’t a sub class of rigidbody2D or Rigidbody2D.

is this have to do with the unity version I’m using?

Yes, that’s outdated; those shortcuts have been removed (aside from transform). Use GetComponent. But angularVelocity is certainly a property of Rigidbody2D; look it up in the docs.

–Eric

1 Like

ok so can you link it I have been looking at the docs and I can’t find how to use angularVelocity
it doesn’t show up in the intelisence

–Eric

@Eric5h5 ok did I mention I am new at unity and C#
I type RigidBody2D. and intellisense doesn’t come up with the suggestion angularVelocity infact it throws an error when I type it in manually

so “Rigidbody2D.angularVelocity = 100f;” in my if statement thows the error
“An object reference is required for the non-static field, method, or property ‘UnityEngine.Rigidbody2D.angularVelocity.get’”
when I run it

Use GetComponent as Eric said.

GetComponent<Rigidbody2D>().angularVelocity = 100.0f;

Rigidbody2D is a class. You need an instance of that class (from GetComponent) to change the angularVelocity on it.

I would suggest learning about object-oriented programming, as it seems you don’t understand what classes/objects are.

Note that in the code you posted in the first post, the shortcut was using GetComponent() behind the scenes for you.

1 Like

ok so how would I store this “GetComponent().angularVelocity= 100.0f;” in a variable?

GetComponent returns a component right?
what is a components’s data type?
I want to store it something like this…
“private “datatype” PhysicsBody = GetComponent()
so I can just call PhysicsBody…angularVelocity = 100.0f;”

the datatype is Rigidbody2D GetComponent() finds the component on your gameobject with the matching type.

@passerbycmc
so something like…
private Rigidbody2d PhysicsBody = GetComponent();?
it returns a error
“C:\Users\Public\Documents\Unity Projects\My First Unity Game\Assets\Resources\Scripts\ShipScript.cs(10,10): Error CS0246: The type or namespace name ‘Rigidbody2d’ could not be found (are you missing a using directive or an assembly reference?) (CS0246) (Assembly-CSharp)”

you had a typo according to that error message, case matters

@passerbycmc
ok I changed Rigidbody2d to Rigidbody2D
but now it says…
C:\Users\Public\Documents\Unity Projects\My First Unity Game\Assets\Resources\Scripts\ShipScript.cs(36,36): Error CS0120: An object reference is required for the non-static field, method, or property ‘UnityEngine.Component.GetComponent()’ (CS0120) (Assembly-CSharp)
I also tried doing private Rigidbody2D PhysicsBody = gameObject.GetComponent();

GetComponent does not create a instance of the component, it gets a component that already exists on your gameobject, so in unity you would have to add a component of RigidBody2D to the same gameobject as this script is on.

@passerbycmc
I do have a rigidbody2d component attached to by game object

You usually want to do something like this (note that there are many other ways to do this as well):

public class MyClass : MonoBehaviour {

    // Create a reference
    Rigidbody2D rb;

    // Assign to the reference
    void Start(){
        rb = GetComponent<Rigidbody2D>();
        rb.angularVelocity = 100f;
    }

}
2 Likes

thankyou
can you explain why it was throwing the error though?
I want to be able to learn from it

It was throwing an error because in the new Unity (not exactly sure which version) they deprecated the “rigidbody2D” property. They didn’t remove the property, and they also stopped assigning the rigidbody2D to it, so it is always null.

well they should of removed it