I just started learning C# and Unity (this is my second day). I have no previous programming experience, so I’m just trying to wrap my head around the logic of the code.
I watched a tutorial yesterday and then decided to try and replicate it today. I needed to check back a couple times but in the end, I got this little script that simply adds two numbers together and returns the result in the Debug log:
public class Test : MonoBehaviour
{
public void Start()
{
int numberVar = PlusTwo(10,5);
Debug.Log(numberVar);
}
public int PlusTwo (int var1, int var2)
{
int resultVar = var1 + var2;
return resultVar;
}
}
I understand the logic of the code somewhat - that it’s creating an integer variable (numberVar) which uses the PlusTwo function and a set of parameters (in this case, 10 and 5).
This function adds together those parameters and returns the result, which is then published in the Debug log.
There are a few things I don’t follow:
Why do we need to make the second variable (resultVar)?
How does the script know what var1 and var2 represent for the PlusTwo function?
How does the script know that when we return resultVar, that’s what it needs to put in the Debug log?
Since the script is set to run at the start, how does it carry out the PlusTwo function and then display the result in the DebugLog? Surely, since the first bit of code is set to run straight away (before the PlusTwo function has run), it wouldn’t know what to do?
They’re probably very basic questions but it is my second ever day so please bear with me!
First of all, some important info. You will see the term function and method all over the place. A method is basically the same as a function, but it is part of a class. So Start and PlusTwo from your example are actually methods, as they belong to your Test class.
You don’t need that resultVar. It’s just good practice to have a special variable for the return value, but that is normally only used when your function is larger and as such more complex. It’s purely for the comfort of the programmer.
In the end there is no difference between your version of PlusTwo and this one:
public int PlusTwo (int var1, int var2)
{
return var1 + var2;
}
It knows, because the function signature tells it that these are of integer type.
public int PlusTwo (int var1, int var2)
It also tells the compiler what you want to return from it, which is also int in this case.
The PlusTwo function returns an integer. You assign that to the integer numberVar and then pass it to the Log method of the Debug class. You are lucky, because someone implemented a version of the Log method that takes an integer as it’s first parameter.
Now this is where i was confused when i started with Unity. There are special methods on the MonoBehaviour class, that are called automatically. The Start method is one of them.
In the case of your example that means:
When you press the Play button in Unity, the engine does it’s startup things. Then it looks for the stuff you have made, which would be some GameObject that you have put in your scene with your Test script on said GameObject. Unity finds the ‘magic’ Start method, which will then be executed.
What you need to understand is that a class is not at all the same as a function/method. It does not run from top to bottom, as you would expect. It is merely a collection of functions, variables, maybe subclasses and whatnot.
See this for the magic methods the MonoBehaviour class gives you (Start, Update, FixedUpdate, LateUpdate, OnGUI, OnDisable, OnEnable). Also make sure to bookmark this, as it will shed some light of the magic things Unity does for you.
Thanks for taking the time to reply - a lot of what you said there is super helpful and has cleared it up for me - especially your answer to my first question about variables.
If I may, there are a couple of things you mention that raise further questions for me (sorry!)
In regards to how the script knows what var1 and var2 are, I’m still unclear on how it knows that var1 is 10 and var2 is 5, because I don’t write anywhere that var1 = 10 and var2 = 5. If I had more code with other variables and integers, how would it still know that when I say var1, I mean 10?
I’m not sure I understand the part about me being lucky, in regards to the Debug log taking an integer as its first parameter.
If I understand correctly, in regards to it knowing which code to execute at what time, you’re saying that it would read the Start code, then scan the rest of the code before running?
So, a function within a class is a method? Just out of curiosity, could I have several classes in the same script - and why might I need to do that?
Don’t be sorry, i actually like answering questions that have enough effort put into them to tell me that there is a chance of success on the other end
You are calling the method with 2 parameters
int numberVar = PlusTwo(10,5);
and you have defined the method as
public int PlusTwo (int var1, int var2)
{
int resultVar = var1 + var2;
return resultVar;
}
The compiler is smart enough to know that 10 and 5 are integers, so it needs to look for a definition of the PlusTwo method with 2 parameters, both of type integer. And you are lucky, because that is just what you have defined
The important takeaway here is that method parameter names are only valid inside the method.
The means of communication which parameter gets what value is through calling the method, like PlusTwo(10,5)
I had a hard time understanding what you actually wanted to know, so i improvised. What i meant is that you can only call Debug.Log with your integer, because somewhere in a class called Debug, there is a method defined as something like Log(int thingToLog). Thus the compiler can actually take your integer and log it to the console.
You have to know that all of the code in your project, be it Unity’s code or your own is read by the compiler long before anything is executed. Everything has to be analyzed and prepared, so that it can eventually be executed. In a ‘normal’ application, you would have a Main method, which is the only point where a program starts to execute.
In the case of Unity, you don’t have that Main method. Unity needs it to start itself and our code is then started when Unity is ready to do so. Unity let’s us start our code via said magic methods.
In C# there are no real functions, as everything is part of a class. Well now it seems i confused you instead of clearing things up…sorry.
99% of the time i would stick to one class per file. Keep it that way for now, it will make life easier for you.
Also any class deriving from MonoBehaviour has to be in a file that has the exact same name as the class. In your case Test. This is due to the whole ‘magic’ stuff Unity does for you. Just accept it for now, until you are more comfortable with programming in a few weeks or so.
Hi, brilliant! I think I’m clear on everything now - thank you so much for the time and effort.
So, my method (PlusTwo) is designed to add together two integers, which it calls var1 and var2, then return the result.
On Start, my script is calling the PlusTwo method and giving it the parameters of 10 and 5.
This means that those integers would only be used for the PlusTwo method. No other bit of the code would try and use those integers because it’s only calling the PlusTwo method.
So, PlusTwo knows that these parameters are the var1 and var2 it’s waiting for, adds them together and returns the result, which then becomes the value of the integer variable numberVar.
This variable is then written to the Debug log.
That leaves me with one final question: does PlusTwo have to wait on specifically “var1” and “var2” - or could I call those anything?
You can name those whatever you like, same for the name of the PlusTwo method.
e.g.:
public int DontCare(int _someNameThat, int _makesSenseToYou)
{
int ret = _someNameThat + _makesSenseToYou;
return ret;
}
I like so prefix my parameters with _ so that i can tell them apart from variables that i declare inside the method (ret in this case). Same for variables that are part of the class and can as such be used by any method from within the same class. I like to prefix them with m_ where m stands for member. (Have a go at this, saves me some time writing it up )
Naming conventions like that are the cause of bloodshed and death throughout any programming community, as everyone has their own preferred way to name things. Important is that YOU can make sense of it, the compiler doesn’t care about those names. Also the length of stuff like class, method and variable names does not have any impact on performance, it’s really just ‘for looks’.
Absolutely brilliant. Thank you so much for taking the time there to explain it all to me - I can only imagine how mundane it is to go over the most basic elements of C# repeatedly!
Now, I fully understand my code! With the help of your responses, I’ve even put together a blog post explaining my code to others, because for me, teaching is the best way of learning.