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 unity_o1SN8KamnOZrSQ · Aug 05, 2018 at 11:48 AM · variablestutorialfunctions

Variables and Functions {Tutorial Question}

Hi Everybody!

I'm just new with Unity and I was trying lo learn something from a tutorial. This one:

https://unity3d.com/learn/tutorials/topics/scripting/variables-and-functions?playlist=17117

And here's the script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class NewBehaviourScript : MonoBehaviour {
 
     int Mario = 5;
 
     // Use this for initialization
     void Start () {
         Mario = MultiplyByTwo(Mario);
         Debug.Log (Mario);
     }
 
     int MultiplyByTwo (int Paolo){
         int calcolo;
         calcolo = Paolo * 2;
         return calcolo;
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 }
 

I gave random names to variables just to see if I got the meaning. I've also understood everything in the the tutorial except for one thing: I still don't understand how in this function:

 int MultiplyByTwo (int Paolo){
     int calcolo;
     calcolo = Paolo * 2;
     return calcolo;
 }

the temporary variable "Paolo" got the value of 5. I'm pretty sure it got it from this variable:

  int Mario = 5;

But how?! The console at the end prints 10 as number.

Thx for your helps.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by GregoryFenn · Aug 05, 2018 at 11:58 AM

You define Mario as an int, and assign it 5. You then doubled it to get a new int, 10. When you passed Mario into that method, the VALUE (not the reference name) of Mario (5) is used to assign the internal value of Paolo.

Paolo doesn’t really mean anything, it is created in the function merely to provide a save storage space that the function takes to input.

Paolo has the same status as calcolo really, as far as the function is concerned.

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 unity_o1SN8KamnOZrSQ · Aug 05, 2018 at 12:13 PM 0
Share

So what's really matter is the fact that I've assigned "int" onto $$anonymous$$ario, right? If yes, I've only have a last question: "What will happen if I will assign 2 integers? How can I decide which one the fuction will chose? For example:

 int $$anonymous$$ario = 5;
 int Carla = 10;

Thanks so much! Your answer was already helpfull for me :)

avatar image unity_o1SN8KamnOZrSQ · Aug 05, 2018 at 12:16 PM 0
Share

Ohh maybe I got the answer by myself... Do i get it from:

 $$anonymous$$ario = $$anonymous$$ultiplyByTwo($$anonymous$$ario);

right?

So i could both run:

 $$anonymous$$ario = $$anonymous$$ultiplyByTwo($$anonymous$$ario);
 Carla = $$anonymous$$ultiplyByTwo(Carla);
avatar image GregoryFenn unity_o1SN8KamnOZrSQ · Aug 05, 2018 at 08:00 PM 0
Share

I think you've almost got it!. In C#, and most object-orientated program$$anonymous$$g languages like Python or C++, is makes sense to write.

 $$anonymous$$ario = $$anonymous$$ultiplyByTwo($$anonymous$$ario);

or

 x = x + 1;

where both x and $$anonymous$$ario are earlier (i.e. above in the code) defined as some integers, e.g. 5 and -7.

So yes, you started by setting $$anonymous$$ario to 5, and then by running the code

 $$anonymous$$ario = $$anonymous$$ultiplyByTwo($$anonymous$$ario);

you simply found a new number that is double $$anonymous$$ario (10 (=2*5)) and then immediately reassigned $$anonymous$$ario to this new number.

If you were to run that line of code again, $$anonymous$$ario would thereafter be 20.

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

96 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 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

Multiple variables in a function? Like function test (number : float , number1 : float) 0 Answers

Passing variable name into function from different script/animation event editor 0 Answers

How to pass a game object through to a function? 2 Answers

How to run a function without certain input variables? 0 Answers

How can you add a function to a variable. Such as string.ToUpper() ? 1 Answer


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