Skip to content

Commit 15f7dea

Browse files
correctly handling componentData
1 parent 6d63c6b commit 15f7dea

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

Editor/Tools/UpdateComponentTool.cs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -97,30 +97,33 @@ public override JObject Execute(JObject parameters)
9797
}
9898

9999
component = Undo.AddComponent(gameObject, componentType);
100+
101+
// Ensure changes are saved
102+
EditorUtility.SetDirty(gameObject);
103+
if (PrefabUtility.IsPartOfAnyPrefab(gameObject))
104+
{
105+
PrefabUtility.RecordPrefabInstancePropertyModifications(component);
106+
}
100107
wasAdded = true;
101108
McpLogger.LogInfo($"[MCP Unity] Added component '{componentName}' to GameObject '{gameObject.name}'");
102109
}
103-
104-
string errorMessage = "";
105-
bool success = false;
106110
// Update component fields
107111
if (componentData != null && componentData.Count > 0)
108112
{
109-
success = UpdateComponentData(component, componentData, out errorMessage);
110-
}
111-
112-
// If update failed, return error
113-
if (!success)
114-
{
115-
if (wasAdded)
113+
bool success = UpdateComponentData(component, componentData, out string errorMessage);
114+
// If update failed, return error
115+
if (!success)
116116
{
117-
return McpUnitySocketHandler.CreateErrorResponse(
118-
$"Successfully added component '{componentName}' to GameObject '{gameObject.name}' BUT\n" +
119-
errorMessage, "component_error");
120-
}
121-
else
122-
{
123-
return McpUnitySocketHandler.CreateErrorResponse(errorMessage, "update_error");
117+
if (wasAdded)
118+
{
119+
return McpUnitySocketHandler.CreateErrorResponse(
120+
$"Successfully added component '{componentName}' to GameObject '{gameObject.name}' BUT\n" +
121+
errorMessage, "component_error");
122+
}
123+
else
124+
{
125+
return McpUnitySocketHandler.CreateErrorResponse(errorMessage, "update_error");
126+
}
124127
}
125128
}
126129

@@ -275,7 +278,7 @@ private bool UpdateComponentData(Component component, JObject componentData, out
275278
JToken fieldValue = property.Value;
276279

277280
// Skip null values
278-
if (fieldValue.Type == JTokenType.Null)
281+
if (string.IsNullOrEmpty(fieldName) || fieldValue.Type == JTokenType.Null)
279282
{
280283
continue;
281284
}

0 commit comments

Comments
 (0)