Forum rules - please read before posting.

Action to Find a gameObject and a component to enable/disable it

Hi, with the help of ChatGPT I managed to get this action that can find a gameObject in the scene (by its name) and a component attached to it (also by its name) to enable or disable it.

When there are multiple gameObjects with the same name in the scene, the script will stop at the first instance it finds.

Add it to your custom actions folder to the Custom actions in the AC window.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

#if UNITY_EDITOR
using UnityEditor;
#endif

namespace AC
{

    [System.Serializable]
    public class ActionFindObjectAndToggleComponent : Action
    {
        public string objectName = "";
        public string componentName = "";
        public bool setActive = true;

        public ActionFindObjectAndToggleComponent ()
        {
            isDisplayed = true;
            category = ActionCategory.Custom;
            title = "Find Game Object Component";
            description = "Find a gameObject in the scene and enable or disable the component attached.";
        }

        public override float Run()
        {
            GameObject obj = GameObject.Find(objectName);

            if (obj != null)
            {
                Component comp = obj.GetComponent(componentName);

                if (comp != null)
                {
                    if (comp is Behaviour behaviour)
                    {
                        behaviour.enabled = setActive;
                    }
                    else if (comp is Collider collider)
                    {
                        collider.enabled = setActive;
                    }
                }
                else
                {
                    ACDebug.LogWarning("Component " + componentName + " not found on " + objectName);
                }
            }
            else
            {
                ACDebug.LogWarning("GameObject " + objectName + " not found in scene");
            }

            return 0f;
        }

#if UNITY_EDITOR
        public override void ShowGUI()
        {
            objectName = EditorGUILayout.TextField("Object Name:", objectName);
            componentName = EditorGUILayout.TextField("Component Name:", componentName);
            setActive = EditorGUILayout.Toggle("Set Active:", setActive);

            AfterRunningOption();
        }
#endif
    }

}

Comments

  • This got me started on something... so I changed this a bit to make it reference the GameObject directly... which means you can drag and drop objects and components onto the action rather than needing to type them out.

    I also added a toggle checkbox:

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    
    namespace AC
    {
        [System.Serializable]
        public class ActionFindObjectAndToggleComponent : Action
        {
            public GameObject targetObject; // Reference to the GameObject
            public Component targetComponent; // Reference to the Component
            public bool setActive = true;
            public bool toggleComponentActive = false;
    
            public ActionFindObjectAndToggleComponent()
            {
                isDisplayed = true;
                category = ActionCategory.Custom;
                title = "Action Toggle Object Component";
                description = "Find a gameObject in the scene and enable, disable, or toggle the component attached.";
            }
    
            public override float Run()
            {
                if (targetObject != null && targetComponent != null)
                {
                    if (toggleComponentActive)
                    {
                        if (targetComponent is Behaviour behaviour)
                        {
                            behaviour.enabled = !behaviour.enabled; // Toggle the active status of the component
                        }
                        else if (targetComponent is Collider collider)
                        {
                            collider.enabled = !collider.enabled; // Toggle the active status of the component
                        }
                    }
                    else
                    {
                        if (targetComponent is Behaviour behaviour)
                        {
                            behaviour.enabled = setActive;
                        }
                        else if (targetComponent is Collider collider)
                        {
                            collider.enabled = setActive;
                        }
                    }
                }
                else
                {
                    ACDebug.LogWarning("Target GameObject or Component reference is not set in the Inspector");
                }
    
                return 0f;
            }
    
    #if UNITY_EDITOR
            public override void ShowGUI()
            {
                targetObject = (GameObject)EditorGUILayout.ObjectField("Target Object:", targetObject, typeof(GameObject), true);
                targetComponent = (Component)EditorGUILayout.ObjectField("Target Component:", targetComponent, typeof(Component), true);
                setActive = EditorGUILayout.Toggle("Set Active:", setActive);
                toggleComponentActive = EditorGUILayout.Toggle("Toggle Component Active:", toggleComponentActive);
    
                AfterRunningOption();
            }
    #endif
        }
    }
    
  • EDIT - I just realized that this doesn't survive scene changes... so I gotta figure that out (I'm not good at code).

    This got me started on something... so I changed this a bit to make it reference the GameObject directly... which means you can drag and drop objects and components onto the action rather than needing to type them out.

    I also added a toggle checkbox... I just need to

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;
    
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    
    namespace AC
    {
        [System.Serializable]
        public class ActionFindObjectAndToggleComponent : Action
        {
            public GameObject targetObject; // Reference to the GameObject
            public Component targetComponent; // Reference to the Component
            public bool setActive = true;
            public bool toggleComponentActive = false;
    
            public ActionFindObjectAndToggleComponent()
            {
                isDisplayed = true;
                category = ActionCategory.Custom;
                title = "Action Toggle Object Component";
                description = "Find a gameObject in the scene and enable, disable, or toggle the component attached.";
            }
    
            public override float Run()
            {
                if (targetObject != null && targetComponent != null)
                {
                    if (toggleComponentActive)
                    {
                        if (targetComponent is Behaviour behaviour)
                        {
                            behaviour.enabled = !behaviour.enabled; // Toggle the active status of the component
                        }
                        else if (targetComponent is Collider collider)
                        {
                            collider.enabled = !collider.enabled; // Toggle the active status of the component
                        }
                    }
                    else
                    {
                        if (targetComponent is Behaviour behaviour)
                        {
                            behaviour.enabled = setActive;
                        }
                        else if (targetComponent is Collider collider)
                        {
                            collider.enabled = setActive;
                        }
                    }
                }
                else
                {
                    ACDebug.LogWarning("Target GameObject or Component reference is not set in the Inspector");
                }
    
                return 0f;
            }
    
    #if UNITY_EDITOR
            public override void ShowGUI()
            {
                targetObject = (GameObject)EditorGUILayout.ObjectField("Target Object:", targetObject, typeof(GameObject), true);
                targetComponent = (Component)EditorGUILayout.ObjectField("Target Component:", targetComponent, typeof(Component), true);
                setActive = EditorGUILayout.Toggle("Set Active:", setActive);
                toggleComponentActive = EditorGUILayout.Toggle("Toggle Component Active:", toggleComponentActive);
    
                AfterRunningOption();
            }
    #endif
        }
    }
    
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.