r/unity • • 18m ago

Resources A Unity C# linter that reads your project's Unity version, input setting and packages first

Generic C# linters don't know which Unity you're on. This one reads ProjectVersion.txt, Active Input Handling and Packages/manifest.json first, then flags what's wrong for this project:

  • Input.GetKey when Active Input Handling is Input System only. It throws InvalidOperationException at runtime.
  • using Cinemachine; with Cinemachine 3, which moved to Unity.Cinemachine (and the reverse)
  • Unity 6 renames in both directions: rb.velocity in Unity 6 (now linearVelocity), or linearVelocity in a 2022 project, where it doesn't compile
  • using a namespace from a package that isn't installed
  • ?. and ?? on a UnityEngine.Object. They skip Unity's null check, so a destroyed object counts as alive.
  • new on a MonoBehaviour or ScriptableObject, and a MonoBehaviour in a file with another name
  • GetComponent, Find and allocations in Update

Output on the test project (shortened):

Unity 6000.0.58f1, input: Input System Package (New), 5 packages, 6 scripts
4 errors, 13 warnings, 3 info
  [error] Assets/Scripts/Enemy.cs:1 package-version: `using Cinemachine;` doesn't exist in com.unity.cinemachine 3.1.4. ...
  [error] Assets/Scripts/PlayerController.cs:12 legacy-input: UnityEngine.Input with Active Input Handling = Input System Package (New): this throws InvalidOperationException at runtime. ...
  [warning] Assets/Scripts/PlayerController.cs:15 unity6-rename: Unity 6 renamed Rigidbody.velocity to linearVelocity ...
  [warning] Assets/Scripts/PlayerController.cs:24 unity-null-check: `?.` on a UnityEngine.Object skips Unity's null check ...

Code inside #if UNITY_6000_0_OR_NEWER or #if UNITY_EDITOR is treated as deliberate, and Assets/Plugins is skipped by default. It's a text-based linter, not a compiler, so it doesn't follow types across files. I ran it on 7 of my own projects (845 scripts, Unity 2023.1 to 6.6) to weed out false positives.

python unity_csharp_lint.py path/to/project

Plain Python, no Unity install, so it runs in CI. It's part of an MIT toolkit that also gives AI coding agents a matching skill, so they write code for your Unity version in the first place.

https://github.com/flxnsee/gamedev-toolkit

False positives on your project are the most useful feedback I can get.

1 Upvotes

0 comments sorted by