Trying to understand variables

Hi guys, I’m currently trying to understand a tutorial of Unity.
It’s about datatypes, but I have a hard time understanding all of it.
The tutorial: DataTypes - Unity Learn

I understand small parts of the code, but the other get’s me confused.
Here is the part I’m talking about: http://imgur.com/a/sjke9

I hope someone can help me and explain exactly what the code means.

Both Vector3 And Transform are “classes”. A class is like a group of different variables and functions that do different tasks. Vector3 is a class that consists of three main variables: x, y and z. These three variables are floating point numbers (numbers that can contain decimals, in programming they are specified as float) and can be anything you specify.

Every class is a kind of object. To create an object in almost all programming languages you have to use the new keyword and the class you want to create, which will look like this: new Vector3 ().

Declaring variables is a very important part of programming. A variable can either be a class (like Vector3, Transform) or a data type like

int (only whole numbers like 1, 2, 3),

float (floating point numbers like 1.0, 2.34, 1)

bool (short for boolean, is either true or false)

There are other data types, this is just a few.

To specify that you want to create a variable you type in the variable type (either class or data type) and then the variable name. A couple examples: float myvariablename;, Vector3 myVector;

To set a variable value you can either do it in one line like this: float myvariablename = 1f; or like this:

float myvariablename;
myvariablename = 1f;

Notice that I used an “f” at the end of the number. This is just something you have to do with floats. Just type in the decimal number you want like 1.33 and add the f (if you don’t you will probably get an error).

You asked why you can’t set a variable value like this: Vector3 pos = (0, 2, 0); This is because you didn’t specify the class to be created. That’s why you have to add in new Vector3.

You also said that you didn’t understand what the code meant. Let’s take a look at it line-by-line:

Vector3 pos = transform.position; Here you are creating a variable called pos and the type is a class named Vector3. You are assigning it a value of transform.position, which is a variable containing the x, y and z position of the current object the script is attached to.

pos = new Vector3(0, 2, 0); You are giving the variable pos a new value which is a new Vector3 with the x value of 0, y value of 2 and z value of 0.

Transform tran = transform; Creating a new class of type Transform called tran. You are assigning it the value of the variable transform which is the position, rotation and scale of the object the script is attached to.

tran position = new Vector3(0, 2, 0) I don’t know what you did here. This code is written wrong and would give you an error. “tran position” means nothing at all. If you removed the “position” you would give the value of the Transform tran a new Vector 3 of xyz (0, 2, 0), but this is also illegal (will throw an error), because the variable tran is a Transform, meaning that you can not assign it a Vector3 (since they are not the same class)

This was a lot of information to read through, and programming is hard to understand at first, but after a while you will get the hang of it. I hope this answers helped you and I would gladly assist you further if you need more help. Just add a comment to this and inside the comment you write “@rubenchris” (without the two ") at the end of the comment, so that I will get a notification :slight_smile:

@rubenchris @ruben_chris

First I want to thank you for taking your time trying to explain me this.
I understand some more of the code now, and also I think I should’ve said I know the ‘basics’ of PHP.
But I still have some questionmarks.

Vector3 pos = transform.position;

I understand that I’m making a new object here of the Vector3 class now.

  • Probally when making an object of the Vector3 class you need to fill in a construct with the variables x,y and z.

But I’m a bit confused with the transform.position part, I have 2 ‘theories’ :

- Theory 1

transform.position = A predefined variable with the variables for x,y,z (0,0,0)
If so, coulnd’t you say this: new Vector3 pos = transform.position; OR new Vector3 pos = (0,0,0); ?

OR

- Theory 2

Vector3 pos = transform.position; // here you take the gameobjects position where this script is attached too

pos = new Vector3(0, 2, 0); // so that you can adjust the position from now on right here

The reason I’m confused is that you make a new object of the Vector3 class with the name pos and then you assign it a value.
And then you immediately assign a new value at the other line.

And the screenshot I took is from a guide of Unity self.
I think you missed the dot operator between tran and position. // tran.position

I got some more questionmarks about the other part of the code, wich I guess you missread. (the dot isn’t good visible)

The tutorial I’m talking about: DataTypes - Unity Learn

PS. I had no internet yesterday so I couldn’t respond.

I hope to hear from you :slight_smile: