Run Unity ml-agents Examples on Mac
Tags: unity3d, machinelearning
Published: 2021-06-20 12:24:08

![Screen Shot 2021-06-20 at 21.15.33](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ed9i7s67kihmil6w7f2i.png)
 

After watching several videos about how reinforcement learning can be demonstrated using Unity's 3D simulated physics, I tried to set up Unity for the first time. After hours of researching and debugging, I finally was able to compile the code to start learning RL. Since this didn't work out of the box and there was no document that summarized the steps and how to debug etc to make this work (some were partial), I am sharing how you can fix these issues.

Firstly, I followed the steps in [this tutorial](https://www.immersivelimit.com/tutorials/unity-ml-agents-setup) to download/install Unity and clone the repository from `ml-agents`: 
At this time, you should have copied `ml-agents/Project/Assets/ML-Agents` folder under `$PROJ_DIR/Assets` folder. 
![Screen Shot 2021-06-20 at 20.40.41](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n10lz0nxgh3dv238ame8.png)

When you press play at the top, it should succeed but in my case, I faced some compile error such as below:


```The type or namespace name 'Input' does not exist in the namespace 'Unity.MLAgents.Extensions'```

This means, the extensions are provided from `Unity-Technologies/ml-agents` repository but this was not copied to the project. So let's copy by going to `Window` -> `Package Manager` -> `plus icon` -> `Add package from disk` -> Select `ml-agents/com.unity.ml-agents.extensions/package.json`. 



![Screen Shot 2021-06-20 at 20.52.08](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0yhcjovj414lavlfumsx.png)

![Screen Shot 2021-06-20 at 20.52.21](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8py4fx0938ax6njl1fwx.png)
  
If this still gives you error, load `ml-agents/com.unity.ml-agents/package.json` and select the correct version according to the [release notes](https://github.com/Unity-Technologies/ml-agents/releases/tag/release_18).

![Screen Shot 2021-06-20 at 20.55.31](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m7ieno7pwbifi5uhkwr0.png)


Then I get an error that `IInputActionAssetProvider` was not read by the compiler, so once I [searched for the code](https://github.com/Unity-Technologies/ml-agents/blob/d34f3cd6ee078782b22341e4ceb958359069ab60/com.unity.ml-agents.extensions/Runtime/Input/IInputActionAssetProvider.cs#L1) and it looks like there is a macro flag `MLA_INPUT_SYSTEM` which enables this to be read. This is only available using the Input System of 1.1.0-preview.3 according to [this code](https://github.com/Unity-Technologies/ml-agents/blob/d34f3cd6ee078782b22341e4ceb958359069ab60/com.unity.ml-agents.extensions/Runtime/Input/Unity.ML-Agents.Extensions.Input.asmdef#L18-L19).

So, I updated Input System's version to `1.1.0-preview.3` in the Package Manager. You cannot preview this version unless you enabled preview in the steps above.

 ![Screen Shot 2021-06-20 at 21.01.58](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p10rfs8qgeb9jn1jfu84.png)
 
Lastly, I came across this error:

```You are trying to read Input using the UnityEngine.Input class, but you have switched active Input handling to Input System package in Player Settings.```

This error occurs because there is a new and old way of handling input system packages. (why is that? I don't know) In my case, this was pointing to a new version whereas it should have been pointing to the old version. So I updated that by going into `Edit` -> `Project Settings` -> `Player` -> `Other Settings` -> `Configuration` -> `Active Input Handling` -> Select `Input Manager (Old)` instead of `Input Manager (New)`. This asks you if you want to restart and so you restart it. The `Both` setting doesn't get applied upon restart so you want to verify the setting after you restart Unity. If someone can tell me why that is, I'd be appreciated.

![Screen Shot 2021-06-20 at 21.05.33](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zakbc6qjrcju75e7czt1.png)

This finally compiled and worked!

  ![ezgif.com-gif-maker (2)](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y5y8yvqxkl0m84ykwo4r.gif)
 

This is my final Environment State:
- ml-agents branch: `release_18`
- Unity ML Agent version: `2.1.0-exp1`
- Unity ML Agent Extension version: `0.5.0-preview`
- Unity Input System version: `1.1.0-preview.3`
- Active Input Handling: Input Manager (Old)


Happy learning!