{"record":{"id":"efa87dd6241fc0a4","repo":"Unity-Technologies/ml-agents","slug":"no-unity-environment-is-loaded","errorCode":null,"errorMessage":"No Unity environment is loaded.","messagePattern":"No Unity environment is loaded\\.","errorType":"exception","errorClass":"UnityEnvironmentException","httpStatus":null,"severity":"error","filePath":"ml-agents-envs/mlagents_envs/environment.py","lineNumber":329,"sourceCode":"                    DecisionSteps.empty(self._env_specs[brain_name]),\n                    TerminalSteps.empty(self._env_specs[brain_name]),\n                )\n        self._side_channel_manager.process_side_channel_message(output.side_channel)\n\n    def reset(self) -> None:\n        if self._loaded:\n            outputs = self._communicator.exchange(\n                self._generate_reset_input(), self._poll_process\n            )\n            if outputs is None:\n                raise UnityCommunicatorStoppedException(\"Communicator has exited.\")\n            self._update_behavior_specs(outputs)\n            rl_output = outputs.rl_output\n            self._update_state(rl_output)\n            self._is_first_message = False\n            self._env_actions.clear()\n        else:\n            raise UnityEnvironmentException(\"No Unity environment is loaded.\")\n\n    @timed\n    def step(self) -> None:\n        if self._is_first_message:\n            return self.reset()\n        if not self._loaded:\n            raise UnityEnvironmentException(\"No Unity environment is loaded.\")\n        # fill the blanks for missing actions\n        for group_name in self._env_specs:\n            if group_name not in self._env_actions:\n                n_agents = 0\n                if group_name in self._env_state:\n                    n_agents = len(self._env_state[group_name][0])\n                self._env_actions[group_name] = self._env_specs[\n                    group_name\n                ].action_spec.empty_action(n_agents)\n        step_input = self._generate_step_input(self._env_actions)\n        with hierarchical_timer(\"communicator.exchange\"):","sourceCodeStart":311,"sourceCodeEnd":347,"githubUrl":"https://github.com/Unity-Technologies/ml-agents/blob/3ecb446f75d1e7400eb404c562dc005d3164cffc/ml-agents-envs/mlagents_envs/environment.py#L311-L347","documentation":"UnityEnvironmentException raised by UnityEnvironment.reset when self._loaded is False, i.e. reset() was called before a Unity environment was successfully loaded (or after it was closed/not yet initialized). The environment has no loaded instance to reset, so the library refuses the call.","triggerScenarios":"Calling env.reset() on a UnityEnvironment constructed but never loaded, or after _close(); or on an instance where initialization failed to set _loaded=True.","commonSituations":"Calling reset in a finally/cleanup block after close(); catching an earlier constructor failure and still attempting reset; using an environment object after its context manager exited.","solutions":["Ensure the UnityEnvironment was constructed successfully with a valid file_name (or a running Editor) before calling reset.","Do not call reset after env.close(); create a new UnityEnvironment instance instead.","Check the environment state before resetting: only reset when env is loaded."],"exampleFix":"// before\nenv = UnityEnvironment('env.x86_64')\nenv.close()\nenv.reset()  # UnityEnvironmentException: No Unity environment is loaded\n\n// after\nenv = UnityEnvironment('env.x86_64')\nenv.reset()\nenv.close()\nenv = UnityEnvironment('env.x86_64')\nenv.reset()","handlingStrategy":"validation","validationCode":"if not getattr(env, '_loaded', False):\n    raise RuntimeError('Cannot reset: no Unity environment is loaded')\nenv.reset()","typeGuard":null,"tryCatchPattern":"from mlagents_envs.exception import UnityEnvironmentException\ntry:\n    env.reset()\nexcept UnityEnvironmentException as e:\n    if 'No Unity environment is loaded' in str(e):\n        print('Construct a valid UnityEnvironment before calling reset().')\n    raise","preventionTips":["Only call reset() after a successful UnityEnvironment construction.","Never call reset() after close(); create a new environment instance.","Initialize environments inside try blocks that skip cleanup-on-failure patterns."],"tags":["lifecycle","environment","reset"],"backgroundTag":"environment-not-loaded","analyzedSha":"3ecb446f75d1e7400eb404c562dc005d3164cffc","analyzedAt":"2026-09-02T16:33:12.832Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}