Execute C# in Editor (sandboxed)
unity_execute_codeCompile and run bare C# statements in the Unity Editor via Roslyn. Returns a job_id to poll for results, enabling scripted editor automation and self-play hooks.
Instructions
Compile and run bare C# statements inside the Unity Editor via Roslyn/AssemblyBuilder. Returns a job_id immediately — poll unity_get_code_result for completion (compile takes ~2-10s).
Code rules: bare statements only (wrapped into static Run(); UnityEngine/UnityEditor/System/LINQ usings included); 'return ;' sends JSON back. Keep snippets short and side-effect aware (runs on main thread; infinite loops hang the Editor). Sandbox blocks: filesystem (System.IO/File./Directory.), processes, network, reflection-load (Assembly.Load/Activator/DllImport), Application.Quit, while(true)/for(;;). Requires: mutations ON + 'Enable C# execution' in Window > Unity MCP (off by default).
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| code | Yes | Bare C# statements executed inside a static Run() (UnityEngine/UnityEditor/System/LINQ usings pre-included). Use 'return <value>;' to send a result back. Example: 'var go = GameObject.Find("Player"); return go.transform.position.ToString();' | |
| timeout_s | No | Job timeout in seconds (10-300, default 60) |