Unity-Technologies/ml-agents · error · UnityGymException

There are no observations provided by the environment.

Error message

There are no observations provided by the environment.

What it means

UnityGymException thrown by UnityGymEnv.__init__ when the environment's single behavior provides neither visual observations (n_vis_obs == 0) nor vector observations (vec_obs_size == 0). The gym wrapper must expose at least one observation space.

Source

Thrown at ml-agents-envs/mlagents_envs/envs/unity_gym_env.py:77

        # Save the step result from the last time all Agents requested decisions.
        self._previous_decision_step: Optional[DecisionSteps] = None
        self._flattener = None
        # Hidden flag used by Atari environments to determine if the game is over
        self.game_over = False
        self._allow_multiple_obs = allow_multiple_obs

        # Check brain configuration
        if len(self._env.behavior_specs) != 1:
            raise UnityGymException(
                "There can only be one behavior in a UnityEnvironment "
                "if it is wrapped in a gym."
            )

        self.name = list(self._env.behavior_specs.keys())[0]
        self.group_spec = self._env.behavior_specs[self.name]

        if self._get_n_vis_obs() == 0 and self._get_vec_obs_size() == 0:
            raise UnityGymException(
                "There are no observations provided by the environment."
            )

        if not self._get_n_vis_obs() >= 1 and uint8_visual:
            logger.warning(
                "uint8_visual was set to true, but visual observations are not in use. "
                "This setting will not have any effect."
            )
        else:
            self.uint8_visual = uint8_visual
        if (
            self._get_n_vis_obs() + self._get_vec_obs_size() >= 2
            and not self._allow_multiple_obs
        ):
            logger.warning(
                "The environment contains multiple observations. "
                "You must define allow_multiple_obs=True to receive them all. "
                "Otherwise, only the first visual observation (or vector observation if"

View on GitHub (pinned to 3ecb446f75)

Solutions

  1. Add at least one Sensor (e.g. CameraSensor or ObservationSensor / implement CollectObservations writing to a VectorSensor) to the Agent in the Unity scene.
  2. Rebuild the executable after adding the sensor.
  3. Inspect group_spec before wrapping: check _get_n_vis_obs() / _get_vec_obs_size() equivalents via behavior_specs observation specs.
  4. If observations exist but under a different behavior, resolve error 50 first (multiple behaviors).

Example fix

// before
env = UnityGymEnv(env_with_no_sensors)
// after
# In Unity: add a CameraSensor or VectorSensor to the Agent, rebuild, then:
env = UnityGymEnv(UnityEnvironment(file_name='rebuilt_app'))
Defensive patterns

Strategy: validation

Validate before calling

spec = next(iter(unity_env.behavior_specs.values()))
vis = sum(len(spec.observation_specs) and 1 for o in spec.observation_specs if any(d >= 3 for d in o.shape))
vec = sum(int(np.prod(o.shape)) for o in spec.observation_specs if all(d < 3 for d in o.shape))
if vis == 0 and vec == 0:
    raise ValueError("Behavior provides no observations")
env = UnityGymEnv(unity_env)

Type guard

def has_observations(group_spec) -> bool:
    return len(group_spec.observation_specs) > 0

Try / catch

try:
    env = UnityGymEnv(unity_env)
except UnityGymException:
    raise RuntimeError("Unity build exposes no sensors/observations; add a sensor to the Agent")

Prevention

When it happens

Trigger: Wrapping a UnityEnvironment whose behavior spec declares no sensors and no vector observation space (Agent has no Sensor components and no VectorSensor configured).

Common situations: Unity scene where the Agent GameObject has no Camera/Raycast/CollectObservations contributions; building an executable from an empty training scene; mismatched environment binary without observation setup.

Related errors


AI-assisted analysis of Unity-Technologies/ml-agents@3ecb446f75 (2026-09-02). Data as JSON: /api/errors/2857ab11dec45677. Report an issue: GitHub.