Skip to content

[Feature]: Auto-sync script component name when user changes the class name in script file #5

@ChenlizheMe

Description

@ChenlizheMe

Area

Rendering

What problem are you trying to solve?

Currently, when a user creates a Python script, attaches it to a GameObject, and later changes the class name inside the script (e.g., from PlayerMovement to NewPlayerMovement), the component reference on the GameObject is lost. The editor shows a missing script error, and the user has to manually reattach the renamed component.

Proposed solution

The engine should automatically detect the class name change and update the component reference on the GameObject accordingly. The script should remain attached without requiring manual reattachment.

Alternatives considered

  1. Require manual reattachment – Current behavior. Simple to implement but breaks workflow and is error-prone.
  2. Warn the user and suggest reattachment – Adds a dialog but still requires manual action. Better than silent failure but not a true fix.
  3. Use GUID-based tracking – Store a stable identifier (e.g., in a .meta file next to the script). The engine uses this GUID to find the script regardless of class name changes. This is the most robust solution and matches how Unity handles script renaming.
  4. Use file path as primary key – Simpler than GUID but breaks if the file is moved or renamed. Partial solution.

Additional context

  • The current system uses the class name as the component identifier, which is fragile.
  • A GUID-based approach would require:
    • Generating a GUID when a new script is created.
    • Storing it in a .meta file (or in script metadata).
    • Updating the scene file to reference the GUID instead of the class name.
    • When the engine loads a script, it resolves the GUID to the current class name.
  • This change touches the asset pipeline (AssetDatabase), serialization, and the component instantiation system.
  • It also enables better refactoring support (e.g., moving/renaming script files without breaking references).
  • This feature is especially important for larger projects or team workflows where renaming is common.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions