{"record":{"id":"4739cd5469f92206","repo":"pixijs/pixijs","slug":"the-supplied-container-must-be-a-child-of-the-call","errorCode":null,"errorMessage":"The supplied Container must be a child of the caller","messagePattern":"The supplied Container must be a child of the caller","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/scene/container/container-mixins/childrenHelperMixin.ts","lineNumber":347,"sourceCode":"\n    setChildIndex(child: ContainerChild, index: number): void\n    {\n        if (index < 0 || index >= this.children.length)\n        {\n            throw new Error(`The index ${index} supplied is out of bounds ${this.children.length}`);\n        }\n\n        this.getChildIndex(child); // check if child exists\n        this.addChildAt(child, index);\n    },\n\n    getChildIndex(child: ContainerChild): number\n    {\n        const index = this.children.indexOf(child);\n\n        if (index === -1)\n        {\n            throw new Error('The supplied Container must be a child of the caller');\n        }\n\n        return index;\n    },\n\n    addChildAt<U extends ContainerChild>(child: U, index: number): U\n    {\n        // #if _DEBUG\n        if (!this.allowChildren)\n        {\n            deprecation(v8_0_0, 'addChildAt: Only Containers will be allowed to add children in v8.0.0');\n        }\n        // #endif\n\n        const { children } = this;\n\n        if (index < 0 || index > children.length)\n        {","sourceCodeStart":329,"sourceCodeEnd":365,"githubUrl":"https://github.com/pixijs/pixijs/blob/4b141e3ced7255b39c2114b6bca03421a37b2363/src/scene/container/container-mixins/childrenHelperMixin.ts#L329-L365","documentation":"`getChildIndex(child)` searches `this.children` for the given reference. If `indexOf` returns -1 the child does not belong to this container, so the lookup is impossible and the mixin throws. Other methods (`setChildIndex`, `addChildAt` re-parenting) call this to verify membership first.","triggerScenarios":"Calling `container.getChildIndex(child)` (or `setChildIndex`/`addChildAt`, which internally verify) with a child whose `.parent` is a different container, or which has already been removed. Also triggered by passing a detached/stale reference.","commonSituations":"Re-parenting logic that loses track of current parent; event handlers firing after a child was removed elsewhere; double-removal; using a child reference from one scene in another container's API.","solutions":["Check `child.parent === container` before calling `getChildIndex`.","Use the parent the child actually belongs to: `child.parent.getChildIndex(child)`.","If you only need to remove, call `child.removeFromParent()` instead of looking up the index manually."],"exampleFix":"// before\nconst i = container.getChildIndex(child); // throws if child.parent !== container\n\n// after\nif (child.parent === container) {\n  const i = container.getChildIndex(child);\n} else if (child.parent) {\n  const i = child.parent.getChildIndex(child);\n}","handlingStrategy":"type-guard","validationCode":"function safeGetChildIndex(container: { children: unknown[]; getChildIndex:(c:unknown)=>number }, child: unknown & { parent?: unknown }): number {\n  return child && child.parent === container ? container.getChildIndex(child) : -1;\n}","typeGuard":"function isChildOf(child: { parent?: unknown }, container: unknown): boolean {\n  return !!child && child.parent === container;\n}","tryCatchPattern":"try {\n  i = container.getChildIndex(child);\n} catch (err) {\n  if (err.message.includes('must be a child of the caller')) {\n    // child not in this container; use child.parent instead, or skip\n  } else throw err;\n}","preventionTips":["Always check `child.parent === container` before index operations.","Use `child.removeFromParent()` for removal to avoid manual index lookups.","Be wary of double-processing children after async events that may have moved them."],"tags":["container","children","membership","parent"],"backgroundTag":null,"analyzedSha":"4b141e3ced7255b39c2114b6bca03421a37b2363","analyzedAt":"2026-08-12T17:27:29.507Z","schemaVersion":2},"datasetVersion":"2026-08-12T18:17:37.767Z"}