Hello, first of all, I’m gonna say that I’m new to this forum and Unity too so it would be appreciative if you guys can explain thoroughly
Anyways, here’s the question:
What is void? Since I learn UnityScript for a bit before moving on to C#, I’m guessing its the same as function, just a different name?
What is void Start()? And what is the purpose of it, like what kind of coding would someone use inside it?
What is void Update? And the purpose of it (I would guess that if you want to call a function, it would be placed here but correct me if I’m wrong)?
Also an extra question: Is it always that, if you want to add a new variable, it’s always have to be before void Start? You can never add a new variable inside a void whateverFunction() right?
Forum Related: When I want to insert a script in a forum, how do I place it in the script layout thingy? I mean, I don’t want to type it inside my other text, it’ll look messy, but in that script layout thingy, with all the colors and stuff…
In C#, a function (or method) defines the return data type, then the method name, then the parameter list. “void” just means that no data is returned.
So: bool myFunction(int parameter)
will return a boolean value (true/false).
But: void myFunction(int parameter)
does not return any value.
Start is code to execute on the very first frame, and Update executes on every frame.
Variables are “scoped” in C# … so if you define them at the class level (outside of a method) then they’re visible to the entire class (and if you put “public” in front of them, they’re visible outside the class, too). If you define a variable inside a method, it’s only visible to the code inside that method.
To format code blocks, put the word “code” inside [ square brackets ] at the start, and close it with /code in square brackets.
So in programming there are things called ‘methods’ (unitscript/javascript you might say ‘function’). A method is some code that operates. There are 2 types of methods, a routine, and a function (there’s that js/us word).
A function means that the method returns a value when it’s complete. It’s like in math, an algebraic function is defined as for any given inputs there is a single repeatable output.
A routine is a method that operates with no output. It performs a job, a routine.
In C#, if your method has an output you write the output as the type of the method:
Now if you write a routine, a method with no output type, you put ‘void’. This means the method has NO return value.
public void DoSomething()
{
}
Note how this naming mirrors that of variables and fields:
accessor variableType variableName;
or
public int Score;
The defining syntactical clarification of a method vs a field is that the method has parenthesis. Which also matches the syntax of properties, which look like fields, but with curly brackets like methods have, symbolizing that it’s like a field, but like a method… which it is. Properties are methods that can be treated like fields.
Start is the method that is called the first time the script starts. You would use this to initialize the state of your script usually.
Update is the method that is called every frame. If you want to operate code every frame (to like move something across the screen), you’d do so in ‘Update’.
Variables can be anywhere. If you want that variable to be what is called a ‘member field’ of your class/script, then it must be scoped to the class. Scoped is when it’s written within the curly brackets of the class/script, but not in the curly brackets of any method. If it’s inside the method’s curly brackets, then it’s a variable scoped to that method and only that method. Curlies are what define scope in C# (and us/js).
BUT it doesn’t have to be before ‘Start’, it just usually is for organizational purposes. It really just needs to be outside of any methods:
public class MyScript : MonoBehaviour
{
//normal location of member fields, for organization purposes
public int Score;
void Start()
{
}
//completely valid, will show up in inspector
public bool Toggle;
void Update()
{
//will not show in inspector, the life of this variable is for that of 'Update'.
bool isDown = Input.GetMouseButton(0);
}
}
Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:
How to do tutorials properly, two (2) simple steps to success:
Tutorials are a GREAT idea. Tutorials should be used this way:
Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That’s how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped. Fortunately this is the easiest part to get right: Be a robot. Don’t make any mistakes. BE PERFECT IN EVERYTHING YOU DO HERE!!
If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.
Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.
Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.
Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.
Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!