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 Royy212 · Aug 01, 2016 at 01:34 PM · variables

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: https://unity3d.com/learn/tutorials/topics/scripting/data-types?playlist=17117

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.

Comment
Add comment · Show 2
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 Ruben_Chris · Aug 01, 2016 at 06:04 PM 0
Share

I added an answer. I wrote this comment just to be sure that you would get notified :)

@Royy212

avatar image Royy212 Ruben_Chris · Aug 03, 2016 at 10:52 AM 0
Share

Okay thanks, for newcomers this forum/helproom is a bit hard to understand.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Ruben_Chris · Aug 01, 2016 at 06:03 PM

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 :)

Comment
Add comment · 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
0

Answer by Royy212 · Aug 03, 2016 at 09:57 AM

@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: https://unity3d.com/learn/tutorials/topics/scripting/data-types?playlist=17117

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

I hope to hear from you :)

Comment
Add comment · 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

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

65 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to access variables in another scene? 2 Answers

Grouping variables question 1 Answer

Referencing variables in another script on the same game object 0 Answers

Static Class Strings not truly null? 1 Answer

Counting Activation of specific panels, not updating Variable? 2 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