Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by xcoder5 · Oct 09, 2015 at 01:42 PM · c#programming

C# code understanding problem

Recently I started learning C# and decided to check out some tutorials and I checked out some tutorials (big surprise!). Anyways it was one by Gucio Devs however I understood all of it except for one part. The whole code is like this:

 using UnityEngine;
 using System.Collections;
 
 public class player : MonoBehaviour {
 
  public float speed = 50f;
  public float jumpPower = 150f;
 
  public bool grounded;
 
  private Rigidbody2D rb2d;
 
  void Start () 
  {
   rb2d = gameObject.GetComponent<Rigidbody2D> ();
  }
  
 
  void Update () 
  {
 
  
  }
 
  void FixedUpdate()
  {
   float h = Input.GetAxis('Horizontal');
 
   rb2d.AddForce((Vector2.right * speed) * h);
   
  }
 
 }

the part I am confused in is: private Rigidbody2D rb2d;


I have also encountered this in Brackeys tutorial the basic syntax type he used was: Private (another script name) (variable name);

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Landern · Oct 09, 2015 at 01:46 PM

The private access modifier will make the field in this case private to the class called player. If it was public you would see it in your inspector and other objects that have a reference to an instance of the player class would be able to set/get that field. Its a way of protecting it in some sense or at the very least trying to keep things nice and clean and less busy. Sometimes this is due to the design of the classes, other times for other mentioned reasons. You can see that rb2d is set in the Start method and used internally to that class based off what you have posted. There isn't a reason to expose it, unless you have a need to modify it through another object(s). Think of it as containment by design.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xcoder5 · Oct 09, 2015 at 01:59 PM 0
Share

I got that part but I don't get the point of the line: private Rigidbody2D rb2d; specifically why is Rigidbody2d mentioned?

avatar image Bunny83 xcoder5 · Oct 09, 2015 at 02:08 PM 1
Share

Variable declarations always have the form:

 [access modifier] [static] <VariableType> <VariableName> [= <initialization value>];

There things in [] are optional. So the "Rigidbody2D" is the type of the variable. So the variable can hold a reference to a Rigidbody2D instance.

In your case the variable ist set to the Rigidbody2D component that is attached to the same gameobject this script is attached to. You usually do this in Start to avoid overhead later since GetComponent has to "search" for the component. So your variable is used to cache the reference to this gameobject's Rigidbody2D component.

edit
Just in case you did not realise: A class declaration represents a "type" on it's own. While built-in types like "int", "float", "bool", ... (which are called primitive types) are value types, classes are reference types. So the variable only holds a reference (which behaves similar to a pointer) to an instance of that classtype. If you assign a reference to a variable, you only store the "value" or the reference (like an address).

Value types on the other hand are different. When you assign a "value type" value to a variable you actually copy it's value. Struct declarations, which are similar to classes, also represents own types, however structs are value type. The most common example in Unity would be "Vector3". It's a struct that has 3 float member variables (x,y,z). When you assign a Vector3 to another Vector3 variable you copy it's whole content (the 3 float values)

 Vector3 v1 = new Vector3(1,1,1);
 Vector3 v2 = v1;
 v1.x = 0;
 Debug.Log(v1); // will print (0,1,1)
 Debug.Log(v2); // will print (1,1,1)

class example:

 public class Test
 {
    public float x = 1;
    public float y = 1;
    public float z = 1;
 }
 
 Test inst = new Test();
 Test secondVariable = inst;
 inst.x = 0;
 Debug.Log(inst.x); // prints "0"
 Debug.Log(Test secondVariable); // prints also "0"


avatar image
1

Answer by brazmogu · Oct 09, 2015 at 02:10 PM

That's just basic C#, really:

  • private means this variable can only be accessed by this class. You can't access it from outside an instance of this class;

  • RigidBody2D is the type of the variable. In case you haven't noticed, Unity creates a script file with a class declaration with the same name of the script(as such: public class ScriptName). Then you can refer to instances of this class by declaring a variable with its name. RigidBody2D is one of Unity's many built-in classes;

  • and rb2d is the variable name, but that you know already.

If your problem is about what RigidBody2D is, I suggest you study Unity's scripting reference, and also basic C# object oriented programming.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Oct 09, 2015 at 02:24 PM 1
Share

Then you can create instances of this class by declaring a variable with its name

You might want to fix this. Declaring a variable doesn't create an instance. It creates a variable that can hold a reference to an instance.

avatar image xcoder5 · Oct 09, 2015 at 02:34 PM 0
Share

sorry to bother you again but is Player$$anonymous$$otor also a variable type ? like: private Player$$anonymous$$otor motor;

avatar image brazmogu xcoder5 · Oct 09, 2015 at 03:14 PM 0
Share

If there is some other code included in the project declaring the class Player$$anonymous$$otor, then yes, it is a type.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Programming in 2d games 0 Answers

What is wrong with this code? 2 Answers

RequireComponent not adding in components 1 Answer

FixedUpdate logic step by step 1 Answer

How can I add an article to a noun in this C# code? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges