@@ -82,7 +82,6 @@ public override JObject Execute(JObject parameters)
8282
8383 // Try to find the component by name
8484 Component component = gameObject . GetComponent ( componentName ) ;
85- bool wasAdded = false ;
8685
8786 // If component not found, try to add it
8887 if ( component == null )
@@ -97,31 +96,41 @@ public override JObject Execute(JObject parameters)
9796 }
9897
9998 component = Undo . AddComponent ( gameObject , componentType ) ;
100- wasAdded = true ;
99+
100+ // Ensure changes are saved
101+ EditorUtility . SetDirty ( gameObject ) ;
102+ if ( PrefabUtility . IsPartOfAnyPrefab ( gameObject ) )
103+ {
104+ PrefabUtility . RecordPrefabInstancePropertyModifications ( component ) ;
105+ }
106+
101107 McpLogger . LogInfo ( $ "[MCP Unity] Added component '{ componentName } ' to GameObject '{ gameObject . name } '") ;
102108 }
103-
104109 // Update component fields
105110 if ( componentData != null && componentData . Count > 0 )
106111 {
107- UpdateComponentData ( component , componentData ) ;
108- }
109-
110- // Ensure changes are saved
111- EditorUtility . SetDirty ( gameObject ) ;
112- if ( PrefabUtility . IsPartOfAnyPrefab ( gameObject ) )
113- {
114- PrefabUtility . RecordPrefabInstancePropertyModifications ( component ) ;
112+ bool success = UpdateComponentData ( component , componentData , out string errorMessage ) ;
113+ // If update failed, return error
114+ if ( ! success )
115+ {
116+ return McpUnitySocketHandler . CreateErrorResponse ( errorMessage , "update_error" ) ;
117+ }
118+
119+ // Ensure field changes are saved
120+ EditorUtility . SetDirty ( gameObject ) ;
121+ if ( PrefabUtility . IsPartOfAnyPrefab ( gameObject ) )
122+ {
123+ PrefabUtility . RecordPrefabInstancePropertyModifications ( component ) ;
124+ }
125+
115126 }
116-
127+
117128 // Create the response
118129 return new JObject
119130 {
120131 [ "success" ] = true ,
121132 [ "type" ] = "text" ,
122- [ "message" ] = wasAdded
123- ? $ "Successfully added component '{ componentName } ' to GameObject '{ gameObject . name } ' and updated its data"
124- : $ "Successfully updated component '{ componentName } ' on GameObject '{ gameObject . name } '"
133+ [ "message" ] = $ "Successfully updated component '{ componentName } ' on GameObject '{ gameObject . name } '"
125134 } ;
126135 }
127136
@@ -236,16 +245,19 @@ private Type FindComponentType(string componentName)
236245 /// <param name="component">The component to update</param>
237246 /// <param name="componentData">The data to apply to the component</param>
238247 /// <returns>True if the component was updated successfully</returns>
239- private bool UpdateComponentData ( Component component , JObject componentData )
248+ private bool UpdateComponentData ( Component component , JObject componentData , out string errorMessage )
240249 {
250+ errorMessage = "" ;
251+
241252 if ( component == null || componentData == null )
242253 {
254+ errorMessage = "Component or component data is null" ;
243255 return false ;
244256 }
245-
257+
246258 Type componentType = component . GetType ( ) ;
247- bool anySuccess = false ;
248-
259+ bool fullSuccess = true ;
260+
249261 // Record object for undo
250262 Undo . RecordObject ( component , $ "Update { componentType . Name } fields") ;
251263
@@ -256,7 +268,7 @@ private bool UpdateComponentData(Component component, JObject componentData)
256268 JToken fieldValue = property . Value ;
257269
258270 // Skip null values
259- if ( fieldValue . Type == JTokenType . Null )
271+ if ( string . IsNullOrEmpty ( fieldName ) || fieldValue . Type == JTokenType . Null )
260272 {
261273 continue ;
262274 }
@@ -269,7 +281,6 @@ private bool UpdateComponentData(Component component, JObject componentData)
269281 {
270282 object value = ConvertJTokenToValue ( fieldValue , fieldInfo . FieldType ) ;
271283 fieldInfo . SetValue ( component , value ) ;
272- anySuccess = true ;
273284 continue ;
274285 }
275286
@@ -281,17 +292,16 @@ private bool UpdateComponentData(Component component, JObject componentData)
281292 {
282293 object value = ConvertJTokenToValue ( fieldValue , propertyInfo . PropertyType ) ;
283294 propertyInfo . SetValue ( component , value ) ;
284- anySuccess = true ;
285295 continue ;
286296 }
287297
288- // Try to update field
289- McpLogger . LogWarning ( $ "Field or Property with name '{ fieldName } ' not found on component '{ componentType . Name } '") ;
298+ fullSuccess = false ;
299+ errorMessage = $ "Field or Property with name '{ fieldName } ' not found on component '{ componentType . Name } '";
290300 }
291-
292- return anySuccess ;
301+
302+ return fullSuccess ;
293303 }
294-
304+
295305 /// <summary>
296306 /// Convert a JToken to a value of the specified type
297307 /// </summary>
0 commit comments