Search Unity

How does Unity read scripts?

Discussion in 'Scripting' started by MoistDumpling14, Sep 9, 2018.

  1. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    I am an intermediate scripter and this is my first time using unity. I am getting started off by following the "Roll a Ball" tutorial and needed some clarification. Here is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class PlayerController : MonoBehaviour {
    6.  
    7.     public float speed;
    8.     public Text countText;
    9.     public Text winText;
    10.  
    11.     private Rigidbody rb;
    12.     private int count;
    13.  
    14.     private void Start()
    15.     {
    16.         rb = GetComponent<Rigidbody>();
    17.         count = 0;
    18.         SetCountText();
    19.         winText.text = "";
    20.     }
    21.  
    22.     void FixedUpdate()
    23.     {
    24.         float moveHorizontal = Input.GetAxis("Horizontal");
    25.         float moveVertical = Input.GetAxis("Vertical");
    26.  
    27.         Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
    28.  
    29.         rb.AddForce(movement * speed);
    30.     }
    31.  
    32.     void OnTriggerEnter(Collider other)
    33.     {
    34.         if (other.gameObject.CompareTag("PickUp"))
    35.         {
    36.             other.gameObject.SetActive(false);
    37.             count = count + 1;
    38.             SetCountText();
    39.         }
    40.     }
    41.     void SetCountText()
    42.     {
    43.         countText.text = "Count: " + count.ToString();
    44.         if (count>=13)
    45.         {
    46.             winText.text = "You Win!";
    47.         }
    48.     }
    49. }
    In the private void start function it runs the function all the way at the bottom (SetCountText). How is this possible that unity is able to run this function before it has even read it. Does unity read up to down or does it figure out the functions before it runs them?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,527
    The code is not necessarily read sequentially like that.

    The code goes through multiple steps (and can vary depending the platform you deploy to).

    The general thing is that the C# code is compiled to CIL (Common Intermediate Language). This is a pre-processed code that can be delivered to any platform that supports .net/mono and can then compile that to machine code:
    https://en.wikipedia.org/wiki/Common_Intermediate_Language

    Now depending platform, the CIL might go through various steps.

    If you deploy to Windows without il2cpp support, then the CIL just ends up on the clients machine as a dll file. When the game is played, the mono runtime picks up the CIL and JIT (just-in-time) compiles it to machine code. It does this on demand for the things it must access. This is the "just-in-time" part. It doesn't necessarily compile the CIL for a class/type until some piece of code is reached that requires that type.

    ...

    BUT, if you target a platform that doesn't support this. Or if you force enable il2cpp. Then Unity processes your code through their custom software the converts the CIL to C++ (the il in il2cpp = intermediate langauge... the 2 = to... and cpp = c++... it's IL to CPP). Then the generated C++ code is compiled to the specific platform that you're building for.

    ...

    In the end this isn't like python where the code is interpreted line by line in order. And you can't access a method that has yet to be declared. C# is nothing like python. C# is PRE-compiled, unlike python which is interpreted in order (it actually depends on how you run your py code, but this isn't a conversation about python).

    Nor is it like C/C++ where the compiler is way more strict. There you can get away with forward declaration, but you have to explicitly tell the compiler what's what. C# on the other hand works around this with its compiler since it's not restricted to the decades of historical backwards compatability the C/C++ compiler needs to maintain.

    ...

    TLDR;

    C# doesn't work like that.
     
    Last edited: Sep 9, 2018
  3. MoistDumpling14

    MoistDumpling14

    Joined:
    Sep 8, 2018
    Posts:
    139
    Thanks a bunch
     
  4. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    This is the norm for fully compiled languages then are defined in a different stage then the execution. So all method definitions are available all the time.