Unity FAQ

This page was created to gather different commonly asked questions about Unity, outside of the basics.


Running in headless mode

A common way to speed up the testing, learning or automatic tuning of an approach is to run only the physics and game simulation without rendering the graphics. This is more commonly known as running headless Links to an external site. . It frees up system resources for computations and also allows one to increase the simulation speeds (see below) higher than normally possible.

If such an approach is desired, here is one possible way of running the code in Unity Player - a standalone method for executing outside of the Unity Editor. This method should work the same way on all platforms.

In the Unity Editor, with an open project:

  1. Navigate to File->Build Settings (Ctrl+Shift+B)
    Location of option in File menu.
  2. In the "Build Settings" window:
    1. Choose the platform "PC, Mac & Linux Standalone" .
    2. Tick the "Server Build" option (which enables Headless mode).
    3. Ensure you have selected the correct Target Platform and Architecture (usually correct by default).
      Build Settings menu with highlighted option.
  3. Click "Build" or optionally "Build And Run" if you wish to execute immediately once the build is done.
  4. You will be prompted to choose a Build directory. Since this generates a few folders and some files. I suggest something along the lines of "<project directory>/Builds" (make sure to exclude files from VCS if you are using it for your project).
  5. Once the build is done you can run it any number of times from the executable created in the chosen build directory.

NB! Keep in mind that if you have done changes to your code, you need to build the executable again.
For this, once  you have configured the build for the first time as explained above, you can quickly run builds again by:

File->Build and Run (Ctrl +B)

This will always build as well as run the simulation. If you wish to build without running you have to use the Build Settings menu.

Speed of simulation

The time scale of the simulation can be modified by changing the static variable:
Time.timeScale Links to an external site.

Note that this works independently of whether you are running in headless mode. It can also be used with the normal "Graphics" mode as well,  but you might be subject to limitations based on framerate.
As noted in the documentation this only takes effect after it is called, so it is useful to make the change in a startup function.

Code snippet for changing timescale based on Assignment 1, GameManager.  ('...' indicates redacted code).

public class GameManager : MonoBehaviour {

    ...
   
    void Start () { 
        ...
        Time.timeScale = 10;
        start_time = Time.time;
        ...
    }
    ... 
}

As can be seen in the documentation the default value for this is 1. The minimum is 0.
The variable acts as a multiplier so you can slow down time by using a value between 0 and 1 and speed up time by using a value above 1.
Therefore, the above snippet speeds up the simulation 10 times.

An important thing to keep in mind is that all the scripts of unity will updated at a rate proportional to the timeScale.
Therefore by speeding up the simulation with timeScale, you are also increasing the rate at which all of your scripts are being executed.
This can cause performance issues, depending on your algorithm.

It might be the case that your scripts are able to allow more "simulation steps" to pass by when decisions are not made very often.
In other words, if you wish to change the time length between each step look into
Time.fixedDeltaTime Links to an external site.

This changes the time duration between steps.

Feel free to read more regarding time in the Unity simulations at Unity Documentation: Time Links to an external site..