Naming convention of Rigidbody variable

Rick mentions that if you name the variable for Rigidbody rigidbody, you’ll get a warning that it is taken and it is recommended to pick something else.

I, too, would like to be a bit more clear than naming it “rb”, and I was wondering if the name c_rigidbody or comp_rigidbody —the “c” and “comp” being short for “component”—is technically correct? Is Rigidbody a component?

Just making sure, so that I can start naming things correctly as early as possible :grinning_face_with_smiling_eyes:

rb is fairly standard so if you want to do it the right way, probably stick with rb.

Yes, Rigidbody is a component.

1 Like

Alright, I’ll go with rb if that is considered standard :slightly_smiling_face:

Thank you!

In addition to what Michael wrote: You may name your variables as you wish. The compiler will tell you if a name is invalid. As long as there is nobody to tell you what to do (e.g. your employer’s naming convention), your names are correct/valid by definition if the compiler does not complain.

In this course, we follow one of the most common C# naming conventions, which is also widely used in the Unity community. Names with an underscore are uncommon in C# but you sometimes see names with underscores in the Unity API and elsewhere.

As for me, I prefer to name my instance variable rigidbody because rb is fairly meaningless in my opinion. If the code is more complex, I would not want to guess around what rb might be, especially not there are more names such as as, bc, t, a, b and so on. At some point, the code might become hard to read.

However, sometimes, I’m lazy and name local Rigidbody variables rb. Depending on the complexity of the code, rb might be sufficient. Many people name their Rigidbody variables rb.

For experienced Unity programmers (like your future self), c_ or comp_ do not add any relevant information to the name because, in the context of Unity, Rigidbody is always used as a component. Furthermore, you would have to ask yourself: Why do I need to know that this Rigidbody is a component? Is this information relevant for what I’m doing here in the code?

In the end, it’s a matter of personal preference, though. Make your code as readable as possible, so your future self won’t have to guess around but also will not get bored/confused by irrelevant information.


See also:

2 Likes

Thank you for offering more information on this subject, Nina!

It’s good to know that underscores aren’t very common. It is something I use very sparingly in other languages, so I will keep it to a minimum in C# as well!

In VS Code, the warning also mentions

Use the new keyword if hiding was intended.

Would it be “correct”, in this case, to declare the variable as new Rigidbody rigidbody? I’m not sure how likely that the conflict would actually cause an issue, so perhaps this is unnecessary, especially in a smaller project?

I saw that in the Unity docs they had written m_Rigidbody in an example, but you mentioned that you prefer to go with rigidbody yourself, so I assume that it’s not something that is normally a cause for concern? :slightly_smiling_face:

rigidbody is a variable in the Monobehaviour class. I’m guessing some legacy code.

Yeah, since rigidbody exists in Monobehaviour, you’re technically supposed to use the new keyword to create the rigidbody or a warning will occur.

It’d be Rigidbody rigidbody = new Rigidbody(); (Edit: Actually now I think about it, this probably wouldn’t work… so more like rigidbody = GetComponent(); ) but yeah, you can do that. AFAIK, rigidbody in Monobehaviour is part of legacy code that isn’t used. It would make sense to be for Unity to take it out, but maybe it’s used somewhere. If it is, there’s potential for some conflict or unexpected behaviour to happen. Highly unlikely, but potential.

I believe the usage of m_Rigidbody was a standard to represent a member variable. That’d be fine assuming you’re using it as a member variable. It’s not something that I use or I see used outside old code. Much like _rigidbody name as a private variable.

Just remember, it’s important for the readability of your code to not confuse those who read it.

1 Like

Yes, that would be correct.

The problem with rigidbody is that it was a property like transform many years ago. I think it has been deprecated for almost half a decade. If I remember correctly, another deprecated property is audiosource. Unity still hasn’t removed them, which is the reason why you get those warnings.

Add the new keyboard if you know what you are doing (you do if you know about the deprecated rigidbody). Do not use it just because the compiler complained.

I saw that in the Unity docs they had written m_Rigidbody in an example, but you mentioned that you prefer to go with rigidbody yourself, so I assume that it’s not something that is normally a cause for concern? :slightly_smiling_face:

Yes, there are a few examples where the examples in the Unity API use underscores. Usually, those are older examples.

For your own code, it is advisable to stay consistent with your namings. If you want to use underscores, that’s perfectly fine. However, use them “everywhere”, not just here and there (unless there is a clear rule why you do that).

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms