How do I read this? private Rigidbody rb;
I followed my first tutorial and now I have some doubts about how C# works.
So, private Rigidbody rb; :
"Private" means It won't appear in the inspector and the only way to reach this data is by directly editing it in the script where it is found?
Rigidbody rb means that rb is a variable linked to the game object Rigidbody?
If so then the diference between "Rigidbody rb;" and "int count;" is that the first one the variable is linked to a game object/class/component and the second I simply create a variable and specify its type but with no attach/link to any object?
Edit: Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
Also I'd like to know what the "new" means in this code. The whole code means that the variable movement will recieve a new x,y,z, right? If so why not make it without the "new" expression? setting it to another values isn't enough?
Thank you!
Answer by Commoble · Mar 02, 2017 at 03:57 AM
"Private" means It won't appear in the inspector
Yes, though you can make private variables appear in the inspector if you need to.
and the only way to reach this data is by directly editing it in the script where it is found?
Yes.
Rigidbody rb means that rb is a variable linked to the game object Rigidbody?
No. Rigidbody rb means that your script has a variable named rb, of the type Rigidbody, and this variable may be uninitialized, or hold a null value, or hold a rigidbody that belongs to your script's gameobject, or hold a rigidbody that belongs to another gameobject (if it holds a rigidbody that doesn't belong to any gameobject you probably screwed something up). To link it to the gameobject's rigidbody, you'll either need to link the rigidbody to your script in the inspector, or make a new one and get a reference to it with this.rb = someGameObject.AddComponent<Rigidbody>()
if someGameObject is a gameobject that doesn't already have a rigidbody (you can use this.gameObject
to get a reference to the script's own gameobject).
Rigidbody is a reference type and int is a value type. You can read up on the difference between value types and reference types by googling them, the details aren't important here. Rigidbody is also a UnityEngine.Component, meaning you can add it as a component to a gameobject, and link it to another Component's field in the inspector if you need to. As far as your script's concerned, though, Rigidbody is just another class. Now, all those public fields that you declare in your monobehaviour and set in the inspector, what happens to those is that when the game starts up, and the scene with your gameobject loads up, and the gameobject initializes, and all instances of your classes (your scripts) initialize, Unity fills in all those public fields with the values and references that you set in the inspector in the editor, so your script can fiddle with them.then the diference between "Rigidbody rb;" and "int count;" is
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
Also I'd like to know what the "new" means in this code.
You're creating a new instance of a Vector3 struct. You're also assigning it to the Vector3 variable you just declared.
The whole code means that the variable movement will recieve a new x,y,z, right?
Yes.
If so why not make it without the "new" expression?
Something like Vector3 movement = someOtherVectorThatAlreadyExists;
? You could do that. Additionally since Vector3s are structs and structs are value types, this will just copy the values, and you'll be dealing with two different vectors now (that happen to have the same value).
It should be pointed out that while the 2 first assumptions are true in a way, access modifiers like internal
, protected
, private
and public
are not a Unity feature. They are an integral part of the C# language and many other program$$anonymous$$g languages.
The fact that variables show up or don't show in the inspector depending on these is just a design decision by Unity engineers.
Private members are only accessible whithin the body of the declaring class, so depending on what one means by "a script" , a private variable might or might not be accessible inside that "script".
Answer by Ahsenhein · Mar 02, 2017 at 05:02 AM
Could you explain to me one more thing?
When I add a component to an object, let's say rigidbody and animator to the Player object, does it mean I always need to make this references on start/awake functions to make this component to work?
private void Awake()
{
anim = GetComponent<Animator>();
PlayerRigidbody = GetComponent<Rigidbody>();
}
No, the component works always if it's attached to a GameObject and it's active.
Doing that in the Awake, or Start method, only lets you acces to this components inside the script.
So, for example, an animation controller would work always, but if you do anim = GetComponent(), you can use that script to play or pause the aniimation.
So basicaly I do it If I need to create any logic in that script that needs to use any function of that component.
And, in this example, what data format would anim recieve? I know that if I declare something like Vector3 movement I know that this variable will recieve x, y and Z in this format (X,Y,Z) but for many others I get confused of what I'm setting to that variable.
Another example is this floor$$anonymous$$ask = Layer$$anonymous$$ask.Get$$anonymous$$ask("Floor");. Floor$$anonymous$$ask has the type int so it's an integer number but I don't see how Get$$anonymous$$ask("Floor") would return a number. Does that mean that all $$anonymous$$asks have a number like an ID?
Thank you. I've just started with unity and C# so there are millions of things to learn.
Whenever you get confused, refer to the Unity API docs or c# API depending on which the confusing method is part of.
Basically googling "c# confusingmethodname" or "unity confusingmethodname" works like a charm usually.
Layer$$anonymous$$asks and layers are a Unity feature as well. They are classes just like any classes you could make. Layers are objects that have a name and an integer value. Get$$anonymous$$ask is a method that returns a value based on how you have set up Layers in the Insoector. How exactly the names and integers get deter$$anonymous$$ed is less relevant than knowing what you need to use them for and how, when you do.