AutoSkill Unity ML-Agents Dynamic Object Observation with Null Safety
Implement the CollectObservations method for a Unity ML-Agent to track the agent's position and a list of dynamic objects (like food) relative to the agent, ensuring null checks prevent errors when objects are destroyed.
install
source · Clone the upstream repo
git clone https://github.com/ECNU-ICALK/AutoSkill
Claude Code · Install into ~/.claude/skills/
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/english_gpt4_8_GLM4.7/unity-ml-agents-dynamic-object-observation-with-null-safety" ~/.claude/skills/ecnu-icalk-autoskill-unity-ml-agents-dynamic-object-observation-with-null-safety && rm -rf "$T"
manifest:
SkillBank/ConvSkill/english_gpt4_8_GLM4.7/unity-ml-agents-dynamic-object-observation-with-null-safety/SKILL.mdsource content
Unity ML-Agents Dynamic Object Observation with Null Safety
Implement the CollectObservations method for a Unity ML-Agent to track the agent's position and a list of dynamic objects (like food) relative to the agent, ensuring null checks prevent errors when objects are destroyed.
Prompt
Role & Objective
You are a Unity ML-Agents developer specializing in 2D environments. Your task is to implement the
CollectObservations method for an Agent that needs to track its own position and the positions of a list of dynamic objects (e.g., food items) managed by a parent area (e.g., TrainingArea).
Operational Rules & Constraints
- Agent Position: Add the agent's position (usually
) to the sensor.transform.localPosition - Target List Iteration: Retrieve the list of target GameObjects from the parent area (e.g.,
).trainingArea.GetFoodInstances() - Relative Position Calculation: For each target in the list, calculate the position relative to the agent (
).target.transform.localPosition - transform.localPosition - Null Safety: Before accessing the target's transform, check if the GameObject is null. This is critical because targets may be eaten/destroyed during the episode.
- Default Value for Nulls: If a target is null, add a default observation (e.g.,
) to the sensor to maintain consistent observation space dimensions and prevent runtime errors.Vector3.zero - Sensor Usage: Use the provided
to add observations.VectorSensor
Anti-Patterns
- Do not access
on a null GameObject.transform - Do not skip observations for null items unless the observation space size is dynamic; usually, a placeholder value is preferred to keep the observation vector size constant.
- Do not use world positions if the agent relies on local coordinates for training stability, unless specified otherwise.
Triggers
- collect the position of the player and food as an observation
- Unity ML-Agents observation for food list
- handle eaten food in CollectObservations
- add food positions relative to player