{"success":true,"name":"cache-unity-references-instead-of-calling-find-or-getcompone","description":"Writing Unity code that looks things up every frame","category":"ai-brimind","path":"ai-brimind/cache-unity-references-instead-of-calling-find-or-getcompone/SKILL.md","file":"SKILL.md","content":"---\nname: cache-unity-references-instead-of-calling-find-or-getcompone\ndescription: \"Writing Unity code that looks things up every frame\"\n---\n\n# Cache Unity References Instead of Calling Find or GetComponent in Update\n\n## When to use\nProject is Unity3D, and you are about to write GameObject.Find, FindObjectOfType, GetComponent, or Camera.main inside Update, FixedUpdate, or any per-frame/loop code.\n\n## Example request\nGetComponent<Rigidbody>() inside Update → same result every frame; cache it once in Awake. GameObject.Find(\"Player\") in an enemy's Update → scene-wide string search per frame; a serialized reference or a spawn-time injection already solves it. Camera.main in a loop → it searches by tag; cache it or use a serialized Camera field. Instantiating and destroying bullets every shot → allocation spikes; that need has a name: object pooling.\n\n## Instructions\nAct as a Unity developer who treats every per-frame lookup as a bug until proven necessary. Before writing Find, FindObjectOfType, GetComponent, Camera.main, or string-based lookups: 1) Check where the code runs — if it is in Update, FixedUpdate, LateUpdate, a coroutine loop, or called every frame, the lookup must not stay there. 2) Prefer the existing mechanisms in order: a [SerializeField] reference wired in the Inspector, a reference passed at spawn/Instantiate time, or a lookup done ONCE in Awake/Start and stored in a field. 3) If the need is repeated creation/destruction, name the pattern — object pooling — and check for an existing pooling utility (Unity's ObjectPool<T>) before writing a custom one. 4) If a lookup truly must happen at runtime, justify why no reference can be wired earlier, and still cache the result. If you are unsure whether the target exists at wiring time, ask about the scene/prefab setup instead of falling back to Find. Only keep a runtime lookup once it is confirmed unavoidable, and explain why the cached or injected reference addresses the real cost (per-frame search and allocations), not just style.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #657\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}