advanced_unity_programming_concepts
Differences
This shows you the differences between two versions of the page.
advanced_unity_programming_concepts [2023/02/07 12:30] – created nkassai | advanced_unity_programming_concepts [2023/02/21 09:13] (current) – nkassai | ||
---|---|---|---|
Line 11: | Line 11: | ||
In Session #3 of the Unity crash course, we will cover the following: | In Session #3 of the Unity crash course, we will cover the following: | ||
+ | * Functions | ||
* Classes | * Classes | ||
* Scope Access Modifiers | * Scope Access Modifiers | ||
Line 23: | Line 24: | ||
Please make sure to finish the last session titled " | Please make sure to finish the last session titled " | ||
+ | ===== Functions ===== | ||
+ | |||
+ | Throughout this tutorial, we have utilized **Debug.Log()** to print out information to the terminal. Log() is known as a **function**, | ||
+ | |||
+ | Let's say we wanted to write a script that would print out the value of two integers, and then their sum. Let's create a new script called functions and write this code in Start. | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | Now, let's save this script, remove the Loops component from the Cube, and add our Functions script to it. Then, play the Unity scene! | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | Now, this script works out no problem, but what if we wanted to repeat this process again in Start? Well, we would have to basically copy and paste the code again to get the following result: | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | While this will still work, the code is starting to look a little messy. What if we wanted to repeat this process 10 times? 20? 40? Well, our code would start becoming hundreds of lines long, even though the same 10 lines are repeating over and over again! Let's place the repeated block of code in a function. As I've mentioned previously, functions are used to help reduce code redundancy, but they also have the additional ability to **return** a value. | ||
+ | |||
+ | **dataType FunctionName(parameters){** | ||
+ | |||
+ | reusable code | ||
+ | |||
+ | **}** | ||
+ | |||
+ | For this purpose, we will use the return type of **void** which just means that we do not want this function to return a value. I'll explain this more as we write more functions, but for now, write the following block of code above start: | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | In our function, we have defined two parameters, two integers, a and b. We then have the same block of code as before. Now, if we want to be able to use this function in our code, write the name of the function in Start, pass in the **correct** parameters and save. **Note:** When I say correct parameters, that means the number of parameters and datatypes must match the function! If you pass in three integers, or an integer and a string, our script won't be able to find what function we are talking about. | ||
+ | |||
+ | Now, go ahead and run our Unity scene to see that we get the same output as before. The difference now, is that if you wanted to run that block of code again, all you have to do is write one line instead of the whole block! | ||
+ | |||
+ | Let's change the return type of the function to be an int instead of a void. Once we do so, we get an error saying that this function needs to return something! Instead of printing out the value to the console, let's just return the value of sum. | ||
+ | |||
+ | {{ : | ||
+ | |||
+ | If we were to run this code now, we wouldn' | ||
===== Classes ===== | ===== Classes ===== | ||
advanced_unity_programming_concepts.1675801842.txt.gz · Last modified: 2023/02/07 12:30 by nkassai