Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
  • Help Room /
avatar image
0
Question by dharmesh_b · Sep 21, 2020 at 06:31 PM · scripting problemscript.scripting beginner

Button appears when switching between scenes or quitting game

Scenario:

There are two Scenes - Scene A and B.

In Scene A there is a Watch Ad Reward Button with the below button cool down script.

Then user clicked Watch ad reward button. The button disappears. (I have set button to appear again after 1 hour).

When I go to Scene B and back to Scene A the button get appeared, also when quit the game. (So there is no point of 1 hour timer to appear the button).

What I need here is the button need to appear after 1 hour even when user switching between scene A and B or quit and restarting the game.

 using System;
  using System.Collections;
  using UnityEngine;
  using UnityEngine.UI;
  
  public class ButtonCooldown : MonoBehaviour
  {
      [SerializeField]
      Button myButton;
      [SerializeField]
      float cooldownDuration = 60f;
  
      void Awake()
      {
          // Get a reference to your button
          myButton = GetComponent<Button>();
          
          if (myButton != null)
          {
              // Listen to its onClick event
              myButton.onClick.AddListener(OnButtonClick);
          }
      }
  
      // This method is called whenever myButton is pressed
      void OnButtonClick()
      {
          StartCoroutine(Cooldown());
      }
  
      // Coroutine that will deactivate and reactivate the button 
      IEnumerator Cooldown()
      {
          // Deactivate myButton
          myButton.interactable = false;
          // Wait for cooldown duration
          yield return new WaitForSeconds(cooldownDuration);
          // Reactivate myButton
          myButton.interactable = true;
      }
  }






Comment
Add comment · Show 1
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 dharmesh_b · Sep 23, 2020 at 08:07 PM 0
Share

any help??

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AaronXRDev · Sep 28, 2020 at 04:11 PM

Here is a version of @dharmesh_b script that will use PlayerPrefs to save and reload the countdown timer.

 using System;
 using System.Collections;
 using UnityEngine;
 using UnityEngine.UI;
 
 [RequireComponent(typeof(Button))]
 public class ButtonCooldown : MonoBehaviour
 {
     [SerializeField]
     [Tooltip("Make sure this value is unique for each cooldown button.")]
     private string cooldownKeyName;
 
     [SerializeField]
     [Tooltip("Time in seconds that this button will be disabled after being pressed.")]
     private float cooldownDuration = 60f;
 
     [SerializeField]
     [Tooltip("How often should this button to check to see if it should be enabled.")]
     private float buttonCheckInterval = 1f;
 
     Button myButton;
 
     private float cooldownRemaining, lastKnownTime;
 
     void Awake()
     {
         // Get a reference to your button
         myButton = GetComponent<Button>();
         myButton.onClick.AddListener(OnButtonClick);
 
         if(cooldownKeyName != null && PlayerPrefs.HasKey(cooldownKeyName))
         {
             //If a cooldown key has been set, load it.
             cooldownRemaining = PlayerPrefs.GetFloat(cooldownKeyName);
         }
         else
         {
             //If no key has been defined, set the cooldown to the max.
             cooldownRemaining = cooldownDuration;
         }
 
         if (cooldownRemaining > 0)
         {
             //If there is still time left on the countdown, disable the button
             myButton.interactable = false;
         }
 
         lastKnownTime = Time.time;
         StartCoroutine(Cooldown());
     }
 
     private void OnButtonClick()
     {
         // Deactivate myButton
         myButton.interactable = false;
 
         //Reset this button's cooldown to the maximum and save to player prefs
         cooldownRemaining = cooldownDuration;
 
         //Set the last known time to the current time
         lastKnownTime = Time.time;
 
         PlayerPrefs.SetFloat(cooldownKeyName, cooldownRemaining);
     }
 
     // Coroutine that will reactivate the button 
     IEnumerator Cooldown()
     {
         while(true)
         {
             yield return new WaitForSeconds(buttonCheckInterval);
 
             if (myButton.interactable == false)
             {
                 //If the button iks disabled, countdown until reset
                 cooldownRemaining -= Time.time - lastKnownTime;
                 lastKnownTime = Time.time;
                 if (cooldownRemaining <= 0)
                 {
                     //If the countdown has passed, enable the button
                     cooldownRemaining = cooldownDuration;
                     myButton.interactable = true;
                 }
             }
 
             if (cooldownKeyName != null)
             {
                 //Set the remaining time to this button's cooldown key to player prefs in case they stop the app
                 PlayerPrefs.SetFloat(cooldownKeyName, cooldownRemaining);
             }
         }
     }
 }
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

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

338 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 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 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

Script to random set active a child object? 0 Answers

how can i save highest played level?,how can i save number of highest played level? 0 Answers

How do i make an uppercut attack? 0 Answers

UnityCar - Controlling Speed with a duration? 2 Answers

Move to next animation with clicking the game object 0 Answers


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