Skip to content

Button attribute doesnt work with multi selection #407

@Havie

Description

@Havie

If you cntrl + click multiple gameobjects with a monobehavior that has a [NaughtyAttribute.Button] tag and click the button, it only calls the method for last selected object.

Here is some updated method for a fix that worked for me:

NaughtyAttributes/Assets/NaughtyAttributes/Scripts/Editor/Utility/NaughtyEditorGUI.cs

public static void Button(UnityEngine.Object target, MethodInfo methodInfo)
{
bool visible = ButtonUtility.IsVisible(target, methodInfo);
if (!visible)
{
return;
}

	    if (methodInfo.GetParameters().All(p => p.IsOptional))
	    {
	        ButtonAttribute buttonAttribute = (ButtonAttribute)methodInfo.GetCustomAttributes(typeof(ButtonAttribute), true)[0];
	        string buttonText = string.IsNullOrEmpty(buttonAttribute.Text) ? ObjectNames.NicifyVariableName(methodInfo.Name) : buttonAttribute.Text;

	        bool buttonEnabled = ButtonUtility.IsEnabled(target, methodInfo);

	        EButtonEnableMode mode = buttonAttribute.SelectedEnableMode;
	        buttonEnabled &=
	            mode == EButtonEnableMode.Always ||
	            mode == EButtonEnableMode.Editor && !Application.isPlaying ||
	            mode == EButtonEnableMode.Playmode && Application.isPlaying;

	        bool methodIsCoroutine = methodInfo.ReturnType == typeof(IEnumerator);
	        if (methodIsCoroutine)
	        {
	            buttonEnabled &= (Application.isPlaying ? true : false);
	        }

	        EditorGUI.BeginDisabledGroup(!buttonEnabled);

	        if (GUILayout.Button(buttonText, _buttonStyle))
	        {
	            object[] defaultParams = methodInfo.GetParameters().Select(p => p.DefaultValue).ToArray();
	            // NEW SFD 2/2026: Get all selected objects of the same type
	            System.Collections.Generic.List<UnityEngine.Object> targetsList = new System.Collections.Generic.List<UnityEngine.Object>();
	            Type targetType = target.GetType();
	            foreach (var obj in Selection.objects)
	            {
		            if (obj == null) 
			            continue;
            
					if (obj is GameObject go)
		            {
			            Component component = go.GetComponent(targetType);
			            if (component != null)
			            {
				            targetsList.Add(component);
			            }
		            }
	            }
				UnityEngine.Object[] targets = targetsList.ToArray();
				
	            // If no multi-selection, fall back to single target
	            if (targets.Length == 0)
	            {
	                targets = new UnityEngine.Object[] { target };
	            }

	            // NEW: Iterate through all selected targets
	            foreach (UnityEngine.Object t in targets)
	            {
	                IEnumerator methodResult = methodInfo.Invoke(t, defaultParams) as IEnumerator;
	                
	                if (!Application.isPlaying)
	                {
	                    // Set target object and scene dirty to serialize changes to disk
	                    EditorUtility.SetDirty(t);

	                    PrefabStage stage = PrefabStageUtility.GetCurrentPrefabStage();
	                    if (stage != null)
	                    {
	                        // Prefab mode
	                        EditorSceneManager.MarkSceneDirty(stage.scene);
	                    }
	                    else
	                    {
	                        // Normal scene
	                        EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
	                    }
	                }
	                else if (methodResult != null && t is MonoBehaviour behaviour)
	                {
	                    behaviour.StartCoroutine(methodResult);
	                }
	            }
	        }

	        EditorGUI.EndDisabledGroup();
	    }
	    else
	    {
	        string warning = typeof(ButtonAttribute).Name + " works only on methods with no parameters";
	        HelpBox_Layout(warning, MessageType.Warning, context: target, logToConsole: true);
	    }
	}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions