Rigidbody vs. rigidbody

I am trying to adapt a 3d char controller I found online to my project, but I am having trouble. the Line in the controller is
rigidbody.velocity = movement * speed; //movement is a vector3 and speed is a float.
When I go into unity I can’t type this statement, it ends up with a capital R and when i try and type .velocity it doesn’t show up, I am getting things like .destroy. I recall reading something about need to do gameobject.getComponent but I am not sure what to do. I am using Unity 5.1.2f1. How do I adapt this line of code into my project? and if you can, explain the difference between Rigidbody vs. rigidbody, that would be great. Or if rigidbody isn’t a thing anymore, let me know.

Rigidbody is the class

rigidbody is a property on MonoBehaviour that returns a Rigidbody if one is connected to the GameObject the MonoBehaviour is attached to.

‘rigidbody’ the property has been deprecated in Unity 5, since all it did was forward GetComponent(). This led to confusion about the efficiency of the property, and just bloating up the the interface of MonoBehaviour, so it was removed.

So use ‘GetComponent()’ instead.

And cache it into a local variable if you plan to repeatedly use it, especially in Update.

1 Like

Thank you, so something like
Rigidbody char = GetComponent();
and then when i want to access things like velocity and position would i do
char.velocity
?

This isn’t working I just tried it, it says the symbol char is unrecognized.
i assume it’s like a normal variable Rigidbody is the data type, char is the name i will refer to it with, and it is equal to GetComponent()
is that wrong? How do i initialize the variable?

Note, ‘GetComponent()’ is really ‘this.GetComponent()’. Whatever the type ‘this’ is, must implement the method ‘GetComponent()’.

In this case that would be ‘MonoBehaviour’.

So make sure your code is scoped inside a MonoBehaviour, and is not static.

I don’t think that is my issue, here is a picture.
The error is “Parser error: unexpected symbol ‘char’”
What is wrong with the statement?

2564995--178723--error.PNG

dont use char for the variable name since char is a keyword for a other data type in c#

1 Like

You’re trying to use a keyword. A char is a single character (8-bit or 16-bit depending on implementation).

https://msdn.microsoft.com/en-us/library/x9h8tsay.aspx

I feel retarded lol, that’s a rookie mistake. I’d use the excuse, I usually program in java, but it’s a keyword in java as well :). Anyway, thanks for the help

1 Like

I am trying this now
Rigidbody rBody = GetComponent;
but when I try to access the velocity. rBody.velocity isn’t a thing it doesn’t reognize rBody as a variable

close the method call with parens:

Rigidbody rBody = this.GetComponent<Rigidbody>();

Without the parens, it’s a reference to the function itself.

Ok, I got the variable working in the script, but when I go back into unity I get this error(the first pic) if I use the keyword this.

and I get this error(the second pic) if I don’t use this.

2565171--178737--er1.PNG
2565171--178738--er2.PNG

  1. it sounds like you’re struggling with the basic syntax of C#… you might want to review some tutorials on that.

  2. forums are a text based medium, we also get nice little tags like the ‘code’ tag that let us decorate our text. You don’t have to take screencaps of your code and your errors to show us. You can copy & paste that text into the thread.

It would make it oh so much easier for us to read.

1 Like

My bad about the forum stuff, but I’ve been using c# for a few years, just a bit confused about the unity stuff.

I got the script to compile with no errors, but now when I try to add the script to the prefab of my character, It says “Can’t add script component because the script class cannot be found. Make sure that there are no compile errors and that the file name and class name match.” The names match and unity isn’t giving me any compile errors

Re-check? It’s all case-sensitive.

Are you inheriting from MonoBehaviour (check for a colon followed by that word after the class name)?

The file name has to be the same as the class name. I think it can throw this error.