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 chesterhilly · Mar 12, 2016 at 08:24 PM · c#scripting problemadsdelay

How do I delay an Ad?

So I have have a script that plays an add every 90 seconds. Every time you change scene (which is quite regular) An ad pops up.

Does anyone know how to delay this

Script:

  using UnityEngine;
  using System.Collections;
  using UnityEngine.Advertisements;
  
  public class ShowAds : MonoBehaviour
  {
      float timer;
      void Start()
      {
          Advertisement.Initialize ("<YOUR GAME ID HERE>"); //REMEMBER TO EDIT!!
      }
  
      void Update()
      {
          timer -= Time.deltaTime;
          ShowAd ();
      }
  
      public void ShowAd()
      {
          if (Advertisement.IsReady () && timer <= 0) 
          {
              Advertisement.Show ();
              timer = 90.0f;
          }
      }
  }
Comment
Add comment · Show 2
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 chesterhilly · Mar 12, 2016 at 09:08 PM 0
Share

Basically every time you change scene a video ad pops up straight away. this would be quite annoying and I want to make it so It would be delayed by about a $$anonymous$$ute or so.

avatar image chesterhilly · Mar 12, 2016 at 09:10 PM 0
Share

also to what you said, where do I put or replace the code? (I don't know c#)

2 Replies

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

Answer by Salmjak · Mar 12, 2016 at 08:41 PM

@chesterhilly You can use Time.time % 90 == 0 as the condition. Using Time.time (notice "...since the start of the game" so it shouldn't be affected by scene changes) and the modulo operator.

My example will make it so the ad is displayed every 90 seconds, regardless of scene change (sine Time.time should be "since the start of the game" and not since the start of the scene, there will be no reset.

If you definitely don't want to show an Ad in the beginning of a scene you can use another check Time.timeSinceLevelLoad > 60f (time.timeSinceLevelLoad) this would only show the ad if 60 seconds or more have passed since the scene was loaded.

Now just combine these examples into one if-statement :)

The final code would look something like:

       public void ShowAd()
       {
           if (Time.time % 90 == 0 && Time.timeSinceLevelLoad > 60) //Is the time divisible by 90 with no remainder? Has atleast 60 seconds passed since the scene was loaded?
           {
               Advertisement.Show ();
           }
       }


Comment
Add comment · Show 2 · 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 chesterhilly · Mar 12, 2016 at 09:20 PM 0
Share

Thanks So $$anonymous$$uch!

avatar image Salmjak chesterhilly · Mar 12, 2016 at 09:25 PM 1
Share

@chesterhilly Please mark answer as correct if it works :)

It should be noted that Time.time % 90f == 0 will only be true when it's exactly 0. So if the function is called when Time.time = 90.1 it won't be true. You might go around this by adding a range ins$$anonymous$$d. Such as Time.time % 90f <= 0.01f. But this will ins$$anonymous$$d be true several frames in a row. Oh, btw! Don't forget to also put Advertisement.IsReady() in there! So the code would look like this:

 public void ShowAd()
        {
            if (Advertisement.IsReady() && Time.time % 90f <= 0.01f && Time.timeSinceLevelLoad > 60) //Is the time divisible by 90 with no remainder? Has atleast 60 seconds passed since the scene was loaded?
            {
                Advertisement.Show ();
            }
        }

You might have to add even more checks in there to make sure the an ad is only shown once every 90 second, no more no less.

avatar image
0

Answer by aditya · Mar 13, 2016 at 07:24 AM

you can simply use Invoke("ShowAd", 5f), this will show th ad after 5 seconds (or change it on your needs)

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

121 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

Related Questions

Adding a delay for weapon switching 1 Answer

Unity Ads Banner not working. 0 Answers

Admob script help 0 Answers

Non Spammable Skill/Button 1 Answer

Function delay? 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