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);
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.
I got that part but I don't get the point of the line: private Rigidbody2D rb2d; specifically why is Rigidbody2d mentioned?
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"
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.
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.
sorry to bother you again but is Player$$anonymous$$otor also a variable type ? like: private Player$$anonymous$$otor motor;
If there is some other code included in the project declaring the class Player$$anonymous$$otor, then yes, it is a type.
Your answer
Follow this Question
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