Skip to content

Unable to Set Component Reference Properties via manage_components Tool #816

@weixudong1983

Description

@weixudong1983

Summary

The manage_components tool with set_property action fails when trying to set reference-type fields (e.g., Button component references) on MonoBehaviour components. The tool validation rejects the input format for reference properties.

Environment

  • Unity MCP Tool: Latest version
  • Unity Version: 2022.3.62f1
  • Use Case: Configuring UI button component references in scene

Problem Description

Attempted Operation

Trying to set the button field (a Button component reference) on a custom MenuPanelButton MonoBehaviour:

Tool: manage_components
Action: set_property
Target: GameObject with instanceID
Component Type: Custom MonoBehaviour class
Properties: {"button": {"instanceID": "29690"}}

Code Example

public abstract class MenuPanelButton : MonoBehaviour
{
    [SerializeField] protected Button button;
    // ... other fields
}

Attempted Tool Calls

Attempt 1: Direct instanceID reference

{
  "action": "set_property",
  "target": "29690",
  "component_type": "Watermelon.SquadShooter.MenuInventoryButton",
  "properties": {
    "button": {"instanceID": "29690"}
  }
}

Error:

1 validation error for call[manage_components]
properties
  Input should be a valid dictionary [type=dict_type, input_value='{"button": {"instanceID": "29690"}}', input_type=str]

Attempt 2: String instanceID

{
  "action": "set_property",
  "target": "29690",
  "component_type": "Watermelon.SquadShooter.MenuInventoryButton",
  "properties": {
    "button": "29690"
  }
}

Result: No error, but field not set (likely treated as string, not reference)

Expected Behavior

The tool should accept a reference format like:

{
  "properties": {
    "button": {"instanceID": 29690}
  }
}

Or:

{
  "properties": {
    "button": {"gameObjectInstanceID": 29690}
  }
}

And correctly set the component reference on the target MonoBehaviour.

Current Workaround

Created an Editor script using reflection to set reference fields:

var buttonField = typeof(MenuPanelButton).GetField("button", 
    BindingFlags.NonPublic | BindingFlags.Instance);
buttonField.SetValue(menuButton, buttonComponent);

Suggested Solution

  1. Add support for reference-type properties in set_property action
  2. Define clear format for component/GameObject references (e.g., {"instanceID": 12345})
  3. Add documentation examples for setting reference fields
  4. Consider adding a dedicated set_reference action for reference-type fields

Impact

This limitation prevents automated scene configuration via MCP tools, requiring manual Editor scripting or manual Inspector configuration for any reference-type fields.

Additional Context

  • Primitive types (int, float, string, bool) work correctly with set_property
  • Only reference types (Component, GameObject, Transform, etc.) have this issue
  • The validation error suggests the tool expects a flat dictionary, not nested objects

Reproduction Steps

  1. Create a MonoBehaviour with a reference field:
    public class TestScript : MonoBehaviour
    {
        [SerializeField] private Button myButton;
    }
  2. Add the script to a GameObject in scene
  3. Try to set myButton reference via manage_components tool
  4. Observe validation error or silent failure

Related Use Cases

  • Setting up UI component references
  • Configuring scene object dependencies
  • Automated testing setup
  • Batch scene configuration

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions