Search Unity

Questions about void Start(), void Update() and other stuff

Discussion in 'Scripting' started by OVERPOWERED99, Feb 9, 2016.

Thread Status:
Not open for further replies.
  1. OVERPOWERED99

    OVERPOWERED99

    Joined:
    Feb 4, 2016
    Posts:
    27
    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:

    1. 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?
    2. What is void Start()? And what is the purpose of it, like what kind of coding would someone use inside it?
    3. 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...
     
    nilsims likes this.
  2. MV10

    MV10

    Joined:
    Nov 6, 2015
    Posts:
    1,889
    You're covering a lot of ground here...

    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.
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,539
    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:

    Code (csharp):
    1.  
    2. modifier outputtype methodName(inputType input, inputType2, input2, ...)
    3. {
    4. }
    5.  
    So you might say:
    Code (csharp):
    1.  
    2. public int CalculateScore(int playerId)
    3. {
    4. }
    5.  
    No access modifier implies 'private' in C#.

    Now if you write a routine, a method with no output type, you put 'void'. This means the method has NO return value.

    Code (csharp):
    1.  
    2. public void DoSomething()
    3. {
    4. }
    5.  
    Note how this naming mirrors that of variables and fields:

    Code (csharp):
    1.  
    2. accessor variableType variableName;
    3.  
    or

    Code (csharp):
    1.  
    2. public int Score;
    3.  
    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:

    Code (csharp):
    1.  
    2. public class MyScript : MonoBehaviour
    3. {
    4.  
    5.     //normal location of member fields, for organization purposes
    6.     public int Score;
    7.  
    8.     void Start()
    9.     {
    10.  
    11.     }
    12.  
    13.     //completely valid, will show up in inspector
    14.     public bool Toggle;
    15.  
    16.     void Update()
    17.     {
    18.         //will not show in inspector, the life of this variable is for that of 'Update'.
    19.         bool isDown = Input.GetMouseButton(0);
    20.     }
    21.  
    22. }
    23.  
    [/quote]
     
    Last edited: Feb 9, 2016
  4. OVERPOWERED99

    OVERPOWERED99

    Joined:
    Feb 4, 2016
    Posts:
    27
    Cool, I understand now. Thanks for the answers!

    And for the code layout thingy...
    Code (csharp):
    1.  
    2. void TestCodeThingy()
    3. {
    4.    bool workingCodeThingy = true;
    5. }
    6.  
     
  5. krousty_bat

    krousty_bat

    Joined:
    Oct 9, 2012
    Posts:
    60
  6. OVERPOWERED99

    OVERPOWERED99

    Joined:
    Feb 4, 2016
    Posts:
    27
    krousty_bat likes this.
  7. Naumanz

    Naumanz

    Joined:
    Mar 5, 2022
    Posts:
    1
    what is void update, void start, void fixed update. Can you tell me in understandable form
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,752
    Start here: Learning

    And please don't necro-post to six-year-old threads, it's against forum rules.

    This guy has some great tutorials:

    Imphenzia: How Did I Learn To Make Games:



    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!
     
    MendProj likes this.
Thread Status:
Not open for further replies.