Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Train Agent in Unity with rendering turned off using ml-agents

Discussion in 'ML-Agents' started by unity_ZZovixQiIOD01g, Oct 5, 2020.

  1. unity_ZZovixQiIOD01g


    Oct 5, 2020
    I created a game in Unity and I'm trying to train an agent in this game with ml-agents tool. I implemented algorithms in Python to train agent and for interacting with unity environment I'm using API from official documentation (
    env = UnityEnvironment(file_name=None, seed=1, side_channels=[])
    ). Now I want to turn off rendering to train agent as fast as possible. I found that
    option could be possible solution but when I tried it, Unity couldn't connect to ml-agents Python API (without
    option it works, it connects to address and port and starts training).

    Can you please help me why it doesn't work with this option and how to solve this or is there another way how to do it?

    Command to run Unity which I used:

    "PathToUnityExecutable\Unity.exe" -batchmode -logfile -projectPath "pathToProject" -executeMethod RunGame.PlayGame

    Code for "RunGame" class with "PlayGame" method is below.
    Code (CSharp):
    1. public static class RunGame {
    3.      public static bool isBatchMode = false;
    5.      public static void PlayGame()
    6.      {
    7.          isBatchMode = true;
    8.          EditorSceneManager.OpenScene("Assets/Scenes/Main.unity");
    9.          EditorApplication.EnterPlaymode();
    10.      }
    12.      public static void ExitGame(int errorCode=0)
    13.      {
    14.         if (Application.isBatchMode)
    15.             EditorApplication.Exit(errorCode);
    17.         isBatchMode = false;
    18.       }
    19. }
    Thank you for your answers!
  2. henrypeteet


    Unity Technologies

    Aug 19, 2020
    I am not sure if this helps but you can build your game in headless mode and run training on the executable. This is commonly used so you can not only get the best single-instance speed but also use the --num-envs argument for even more parallel gains. Please let me know if this isn't what you are looking for.
  3. unity_ZZovixQiIOD01g


    Oct 5, 2020

    Yes, this is what I need. Thank you very much!