💻 Coding
Unity C# MonoBehaviour: Unity 6 vs 2022 LTS Fork
Write a production MonoBehaviour with a Unity 6 fork and a 2022 LTS fork, Input System, lifecycle notes, and no Find in Update.
0Reviews
Prompt
Act as a Unity gameplay engineer. Write C# MonoBehaviours that compile. Fork the code for Unity 6 and Unity 2022 LTS. Do not mix APIs. Do not use GameObject.Find or FindObjectOfType inside Update, FixedUpdate, or LateUpdate. Inputs: - Target versions: [Unity 6.x / 2022.3 LTS / both] - Render pipeline: [URP / HDRP / Built-in] - What the script must do: [Behavior] - Input: [old Input Manager / new Input System / both] - Physics: [2D / 3D / none] - SerializeFields I already have: [Fields] - Constraints: [No alloc in tick, no coroutines, etc.] - Editor version I will paste into: [Exact version string] Generate: 1. Version map: APIs that differ (Input System package, FindFirstObjectByType vs FindObjectOfType). State which fork I should paste. 2. Unity 2022.3 LTS script: full file, SerializeField, Awake cache, Update or FixedUpdate as appropriate. 3. Unity 6 script: full file. Use Unity 6-safe APIs. If Input System is requested, show InputAction, not Input.GetAxis unless Inputs say old manager. 4. Inspector contract: every SerializeField, expected null behavior, RequireComponent. 5. Lifecycle comments: why FixedUpdate vs Update, why cache in Awake. 6. Test checklist in Play Mode: 6 steps. No invented package versions. 7. What I refused: Find-in-Update, empty catch, hiding missing refs. Constraints: - Output real C#, not pseudocode. - If Inputs say 2022 LTS only, skip the Unity 6 file and say so. - Do not add Asset Store packages that were not requested. - Call out if Behavior needs a package (Input System, Cinemachine) and name the package, not a version you do not know.
Instructions
Replace every [bracket] with your details before running. Works on ChatGPT, Claude, and Gemini.
Examples
Example Input
Versions: both, I ship 2022.3.50f1 LTS and am trialing Unity 6000.0.31f1 Pipeline: URP Behavior: 3D character, WASD on XZ, camera-relative, sprint on Left Shift, jump on Space, CharacterController, no rigidbody Input: new Input System for Unity 6 fork; 2022 LTS fork may use Input.GetAxis if Input System is not installed there yet Physics: 3D CharacterController Fields: moveSpeed 6, sprintMultiplier 1.6, jumpHeight 1.4, gravity -20, cameraTransform Constraints: no alloc in tick, no Find in Update, cache CharacterController in Awake Editor: see versions above
Example Output
1. Version map
Paste 2022 fork into 2022.3.50f1. Paste Unity 6 fork into 6000.0.31f1. Do not mix.
2022 LTS: Input.GetAxisRaw / GetButtonDown (old manager).
Unity 6: com.unity.inputsystem, InputAction.ReadValue, WasPressedThisFrame. If you must find, FindFirstObjectByType in Awake only.
URP: this script does not touch Render Graph.
2. Unity 2022.3 LTS
using UnityEngine;
[RequireComponent(typeof(CharacterController))]
public sealed class PlayerMove2022 : MonoBehaviour
{
[SerializeField] float moveSpeed = 6f;
[SerializeField] float sprintMultiplier = 1.6f;
[SerializeField] float jumpHeight = 1.4f;
[SerializeField] float gravity = -20f;
[SerializeField] Transform cameraTransform;
CharacterController _cc;
float _vy;
void Awake()
{
_cc = GetComponent<CharacterController>();
if (cameraTransform == null)
Debug.LogError("PlayerMove2022: cameraTransform is not assigned", this);
}
void Update()
{
if (cameraTransform == null) return;
float h = Input.GetAxisRaw("Horizontal");
float v = Input.GetAxisRaw("Vertical");
Vector3 camF = cameraTransform.forward; camF.y = 0f; camF.Normalize();
Vector3 camR = cameraTransform.right; camR.y = 0f; camR.Normalize();
Vector3 wish = camR * h + camF * v;
if (wish.sqrMagnitude > 1f) wish.Normalize();
float speed = moveSpeed * (Input.GetKey(KeyCode.LeftShift) ? sprintMultiplier : 1f);
if (_cc.isGrounded && _vy < 0f) _vy = -1f;
if (_cc.isGrounded && Input.GetButtonDown("Jump"))
_vy = Mathf.Sqrt(jumpHeight * -2f * gravity);
_vy += gravity * Time.deltaTime;
Vector3 delta = wish * speed; delta.y = _vy;
_cc.Move(delta * Time.deltaTime);
}
}
3. Unity 6 (Input System)
using UnityEngine;
using UnityEngine.InputSystem;
[RequireComponent(typeof(CharacterController))]
public sealed class PlayerMoveU6 : MonoBehaviour
{
[SerializeField] float moveSpeed = 6f;
[SerializeField] float sprintMultiplier = 1.6f;
[SerializeField] float jumpHeight = 1.4f;
[SerializeField] float gravity = -20f;
[SerializeField] Transform cameraTransform;
[SerializeField] InputActionReference move;
[SerializeField] InputActionReference jump;
[SerializeField] InputActionReference sprint;
CharacterController _cc;
float _vy;
void Awake()
{
_cc = GetComponent<CharacterController>();
if (cameraTransform == null)
Debug.LogError("PlayerMoveU6: cameraTransform is not assigned", this);
}
void OnEnable() { move?.action?.Enable(); jump?.action?.Enable(); sprint?.action?.Enable(); }
void OnDisable() { move?.action?.Disable(); jump?.action?.Disable(); sprint?.action?.Disable(); }
void Update()
{
if (cameraTransform == null) return;
Vector2 stick = move != null ? move.action.ReadValue<Vector2>() : Vector2.zero;
Vector3 camF = cameraTransform.forward; camF.y = 0f; camF.Normalize();
Vector3 camR = cameraTransform.right; camR.y = 0f; camR.Normalize();
Vector3 wish = camR * stick.x + camF * stick.y;
if (wish.sqrMagnitude > 1f) wish.Normalize();
bool spr = sprint != null && sprint.action.IsPressed();
float speed = moveSpeed * (spr ? sprintMultiplier : 1f);
if (_cc.isGrounded && _vy < 0f) _vy = -1f;
if (_cc.isGrounded && jump != null && jump.action.WasPressedThisFrame())
_vy = Mathf.Sqrt(jumpHeight * -2f * gravity);
_vy += gravity * Time.deltaTime;
Vector3 delta = wish * speed; delta.y = _vy;
_cc.Move(delta * Time.deltaTime);
}
}
4. Inspector
Assign cameraTransform. Unity 6: assign three InputActionReferences. Missing camera: LogError then no-op. RequireComponent adds CharacterController.
5. Lifecycle
CharacterController.Move lives in Update, not FixedUpdate (not a Rigidbody). Cache _cc in Awake. No Find.
6. Play Mode
1 Assign camera. 2 WASD camera-relative. 3 Shift sprint. 4 Space jump grounded only. 5 Unassign camera: error + no move. 6 Profiler: no GC from this script.
7. Refused
Find in Update. Mixing GetAxis into the Unity 6 file. Invented Input System package versions.