SetActiveonClick but with a Delay

Morning all :waving_hand: :grinning_face:

Can someone help? I’m trying to find a way to have a SetActiveonClick function that incorporates a 2 second delay before the target object is affected.

Thanks, in advance, for any advice :smiling_face_with_three_hearts:

To implement a SetActiveOnClick function that includes a 2-second delay before the target object is affected, you can modify the existing OnPointerClick method to include a coroutine for handling the delay.

Here’s a version of how this functionality can be implemented:

using UnityEngine;
using UnityEngine.EventSystems;

namespace Needle.Engine.Components
{
    [AddComponentMenu(USDZExporter.ComponentMenuPrefix + "Set Active on Click with Delay" + USDZExporter.ComponentMenuTags)]
    public class SetActiveOnClickWithDelay : MonoBehaviour, IPointerClickHandler
    {
        public Transform target;
        public bool toggleOnClick = false;
        public bool targetState = true;
        public bool hideSelf = true;

        public void OnPointerClick(PointerEventData eventData)
        {
            StartCoroutine(HandleClick());
        }

        private IEnumerator HandleClick()
        {
            yield return new WaitForSeconds(2f); // Wait for 2 seconds

            if (target)
            {
                var isActive = target.gameObject.activeSelf;

                if (toggleOnClick)
                {
                    target.gameObject.SetActive(!isActive);
                }
                else
                {
                    target.gameObject.SetActive(targetState);
                }

                if (hideSelf)
                {
                    gameObject.SetActive(false);
                }
            }
        }
    }
}

Explanation:

  1. Coroutine: The method HandleClick is defined as a coroutine, allowing the use of yield return to create a delay.
  2. WaitForSeconds: This pauses the execution for the specified duration (2 seconds in this case) before proceeding to toggle the target’s active state.
  3. Toggle Logic: Once the delay is over, the target’s active state is altered based on the toggleOnClick or targetState properties.

Make sure you attach this component to the clickable GameObject and assign the target that you’d like to toggle in the Unity editor.

Let me know if you need help with something else!

1 replies left - Get a PRO license for more AI replies.
thread_ua7hnF19AqeNFJKrQFxMo6pf
Reply to this message to continue the conversation

Thanks, I’ll have a look at that and give it a try.

You’re welcome, Frannie! If you have any further questions or need additional assistance while trying out the code, feel free to ask. Happy coding!

0 replies left - Get a PRO license for more AI replies.
thread_ua7hnF19AqeNFJKrQFxMo6pf
Reply to this message to continue the conversation

No luck. Unfortunately I can’t get that to work… not enough knowledge on my part, I’m afraid.

No worries.

Hi frannie3754, I’m sorry but I can’t help you right now. You have reached the maximum number of replies. :cactus:. Please try again later or get a PRO license for more AI help.
**