{"record":{"id":"4fce32cac547d42b","repo":"Unity-Technologies/ml-agents","slug":"agent-is-already-registered-with-a-group-unregist","errorCode":null,"errorMessage":"Agent is already registered with a group. Unregister it first.","messagePattern":"Agent is already registered with a group\\. Unregister it first\\.","errorType":"exception","errorClass":"UnityAgentsException","httpStatus":null,"severity":"error","filePath":"com.unity.ml-agents/Runtime/Agent.cs","lineNumber":1431,"sourceCode":"            m_ActuatorManager.UpdateActions(actions);\n        }\n\n        internal void SetMultiAgentGroup(IMultiAgentGroup multiAgentGroup)\n        {\n            if (multiAgentGroup == null)\n            {\n                m_GroupId = 0;\n            }\n            else\n            {\n                var newGroupId = multiAgentGroup.GetId();\n                if (m_GroupId == 0 || m_GroupId == newGroupId)\n                {\n                    m_GroupId = newGroupId;\n                }\n                else\n                {\n                    throw new UnityAgentsException(\"Agent is already registered with a group. Unregister it first.\");\n                }\n            }\n        }\n    }\n}\n","sourceCodeStart":1413,"sourceCodeEnd":1437,"githubUrl":"https://github.com/Unity-Technologies/ml-agents/blob/3ecb446f75d1e7400eb404c562dc005d3164cffc/com.unity.ml-agents/Runtime/Agent.cs#L1413-L1437","documentation":"The Agent's group registration property throws UnityAgentsException when the agent already belongs to a group (m_GroupId != 0) and is assigned a different group id without being unregistered first. Group ids are immutable once set to protect the integrity of multi-agent group training.","triggerScenarios":"Setting agent.GroupId (or changing it) to a value different from the current m_GroupId when m_GroupId != 0 — reassigning an agent from one group to another at runtime.","commonSituations":"Dynamically re-teaming agents mid-episode in multi-agent scenarios; spawning/recycling agents that retain a stale group id from a previous episode.","solutions":["Unregister the agent from its current group before assigning a new GroupId","Reuse the same GroupId if the agent's team membership hasn't actually changed","Reset the agent's group id to 0 (or recreate the agent) between episodes"],"exampleFix":"// before\nagent.GroupId = 5; // agent already in group 2\n// after\nagentManager.UnregisterAgent(agent);\nagent.GroupId = 5;","handlingStrategy":"validation","validationCode":"if (agent.GroupId == 0 || agent.GroupId == newGroupId) {\n    agent.GroupId = newGroupId;\n} else {\n    // unregister from current group first\n}","typeGuard":"bool CanJoinGroup(Agent a, int g) => a.GroupId == 0 || a.GroupId == g;","tryCatchPattern":"try { agent.GroupId = newGroupId; } catch (UnityAgentsException e) { Debug.LogError(e.Message); }","preventionTips":["Unregister agents from groups before re-teaming","Reset group ids between episodes when recycling agents","Only set GroupId once per agent lifetime"],"tags":["unity","ml-agents","agent","group-management"],"backgroundTag":"duplicate-registration","analyzedSha":"3ecb446f75d1e7400eb404c562dc005d3164cffc","analyzedAt":"2026-09-02T16:33:12.832Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T21:17:11.164Z"}