Creating an environment
One thing you may have noticed while looking over the last example was that an ML-Agent environment requires a bit of custom setup. Unity documentation recommends that an ML environment be constructed of Academy, Agent, and Brain objects with associated scripts. There is a Template folder in the ML-Agents project which we will use to configure and set up a simple environment. Let's jump back to the Unity editor and get started setting up our first simple ML environment:
- Locate the
Templatefolder in theML-Agentsfolder within theProjectwindow of the editor. - Right-click (Command Click on macOS) on the
Templatefolder and selectShow in Explorerfrom the context menu. This will open anexplorerwindow with the files. - Select and copy the
Templatefolder. - Navigate up two levels to the
Assetsfolder and paste the copied folder. This will add theTemplatefolder to the rootAssetsfolder. - Rename the
Templatefolder toSimple.
Note
When you return to the editor, you will see a few namespace errors due to the duplicate Template scripts. We will fix that shortly.
- Return to the Unity editor and confirm the folder and files have been copied to the new
Simplefolder, as shown in the following screenshot:

Verifying that the Simple folder was created
- Double-click on the
Sceneto open it in the editor.
Renaming the scripts
That sets up the simple scene, but you may have noticed that there are still a few duplicated naming errors. We will need to rename the Template scripts in the Simple/Scripts folder. Follow this next exercise to rename each of the scripts:
- Open the
Scriptsfolder. - Rename each of the files from
TemplatetoSimple, as shown in the following excerpt of theProjectwindow:

Renaming the Template scripts to Simple
- Double-click on of the
SimpleAcademyscript file to open it in your code editor. Rename the class fromTemplateAcademytoSimpleAcademyso that it matches the file name, as shown in the following code:
public class SimpleAcademy : Academy {- Repeat this process for the Agent and Decision scripts. The objects in the scene are still pointing to the template scripts, so we will update that next. Make sure to save all the scripts with your changes before returning to the editor. If all the files are renamed correctly, the naming errors will go away.
- Select and rename the
Ball3DAcademyto justAcademyin theHierarchywindow. - Select the
Academyobject in the Hierarchy window. Click the Gear icon beside theTemplateAcademycomponent in theInspectorwindow and selectRemove Component to remove the script.
- Click the
Add Componentbutton and typeSimplein the component search bar, as shown in the following screenshot:

Adding the SimpleAcademy object to the Academy object
- Click on the
Simple Academyitem, as shown in the preceding excerpt, to add the component to theAcademyobject. - Repeat the process for the
Agentobject and remove theTemplateAgentscript and add theSimpleAgentscript. - After you are done, be sure to save the scene and the project.
Note
It is surprising that Unity didn't provide a better set of editor tools to build a new ML Agent environment, at least not at the time of writing this book. In the source code download for this book (Chapter_1/Editor_Tools), an asset package has been provided that can automate this setup for you. We may decide to put this package and some others from this book on the asset store.
That sets up a new ML environment for us to start implementing our own Academy, Agent, and Decision (Brain) scripts. We will get into the details of these scripts in the next section when we set up our first learning problem.