Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by aeariss · Jan 04, 2018 at 12:54 AM · guibuttonwaitforsecondsdelayonclick

Putting a delay in a button OnClick ? (Javascript)

Hello, this is driving me crazy trying to google a solution.

I'm wanting a delay between these two events within the single click of a button.

The two events closes one canvas (canvas this button is inside) then opens a new canvas.

I want a delay before the next canvas opens so that the camera movement is completed before the canvas opens.


The code below moves my camera

 function MM_CameraPosUpgrade()
 {
 GameObject.Find("Camera").GetComponent.<Transform>().DOMove(Vector3(2.90, 3.69, 10.35),1.5, false);
 }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aeariss · Jan 04, 2018 at 09:11 PM

I've figured it out using the free assest store tween library DOTween. What I want to happen: Click Button -> Camera Moves to New position -> current active canvas is hidden -> New canvas is visible

All of this happens with one click. I dont want the new canvas to open untill the camera has moved to it's new position. My Code looks like this

 var upgrade: GameObject;
 
 function MM_CameraPosUpgrade()
 {
     GameObject.Find("Camera").GetComponent.<Transform>().DOMove(Vector3(2.90, 3.69, 10.35),1.5, false).OnComplete(off);    
         
 }
 
 function off()
 {
     gameObject.SetActive(false);
     upgrade.SetActive(true);
 }

The canvas is stored in var upgrade. I use DOTween API 'DoMove' to move the camera on button click. Then i use another API 'OnComplete' to call another function i've made that will only start once the camera has finished moving.

with DOTween I did see other API where you can delay with specific times in mind, i just needed to start an event when camera finished moving.

if anyone needs help with delaying inside an OnClick then let me know.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by David_Rios · Jan 04, 2018 at 01:06 AM

You can make the OnClick() function call a function that calls a coroutine. Inside the coroutine you can make one event happen then add a delay, and then make the other event happen after the delay.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Lysander · Jan 04, 2018 at 02:56 AM

Just so happens that I already had something laying around that could do this- ripped most of it out for simplicity, but this should do what you want:

 using System.Collections;
 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.Events;
 
 [RequireComponent(typeof(Button))]
 public class DelayedOnClick : MonoBehaviour
 {
     [SerializeField, Range(.01f, 3f)]
     private float _delay = .01f;
 
     [SerializeField, Space(10f)]
     private UnityEvent _onClick;
 
     private Button _thisButton;
     private WaitForSeconds _delayTimer;
 
 
     private void Awake()
     {
         _thisButton = GetComponent<Button>();
 
         // cache the yield instruction for performance
         _delayTimer = new WaitForSeconds(_delay);
     }
 
     private void Start()
     {
         // get alerted when the button is pressed
         _thisButton.onClick.AddListener(() => StartCoroutine(DelayedClickRoutine()));
     }
 
 
     private IEnumerator DelayedClickRoutine()
     {
         // wait the delay time, then invoke the event
         yield return _delayTimer;
         _onClick.Invoke();
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aeariss · Jan 04, 2018 at 08:22 PM 0
Share

Sorry, i forgot to mention that I use javascript. I understand most of this apart from:

 [RequireComponent(typeof(Button))]
  public class DelayedOnClick : $$anonymous$$onoBehaviour
  {
      [SerializeField, Range(.01f, 3f)]
      private float _delay = .01f;
  
      [SerializeField, Space(10f)]

i've not come across require component and serializefield

any chance you know this in javascript?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

140 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to change button image on click 2 Answers

check alpha of gameObject C# 3 Answers

GUI button to Unity5 Button OnClic function 1 Answer

How to WaitforSeconds a GuiButton ? 0 Answers

How do i add a delay to how often a unity ui button may be used? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges