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 hanaan328 · Sep 26, 2018 at 03:15 PM · c#playerprefsadsadvertising

How to show an ad every 4 times my player dies

Hey, right now I have my ads setup so that every time my player dies it shows an ad. However, I want it so that an ad shows every 4 times the player dies. I've searched this up and can't find anything that exactly helps me. Here's what I've got so far:

 private void OnTriggerEnter2D(Collider2D collision
 {
         if (collision.tag == "Obstacle")
         {
             showAd();
         }
 }
 
  private void showAd()
     {
         if (Chartboost.hasInterstitial(CBLocation.Default))
         {
             Chartboost.showInterstitial(CBLocation.Default);
         }
         else
         {
             Chartboost.cacheInterstitial(CBLocation.Default);
         }
     }
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

5 Replies

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

Answer by Stratosome · Sep 26, 2018 at 04:39 PM

Hmm, I typed up a response to this before and sent it, but I guess it's gone? Anyway, As others have said, your player can have a death counter. We can actually use the modulus operator here though to make this a bit cooler. So, if deathCount % 4 == 0, then there is no remainder, and deathCount is a multiple of 4, and we want to show the ad. If deathCount % 4 != 0, it is not a multiple of 4, and we don't want to show the ad. Here's a bit of rough code for ya:

 private int deathCount = 0;
 
 private void KillPlayer() {
      deathCount++;
 
      if (deathCount % 4 == 0) {
           ShowAdvertisement();
      }
 }
 
 private void ShowAdvertisement() {
      // Fancy code to show the ad
 }

With this method, you won't need to bother with resetting some death counter variable to zero.

Comment
Add comment · Show 10 · 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 bennett_apps · Sep 26, 2018 at 04:44 PM 0
Share

Ya...another way to do the same thing!

avatar image hanaan328 · Sep 26, 2018 at 04:58 PM 0
Share

Tried this as well, having the same problem as I mentioned in Coder333's answer

avatar image Stratosome hanaan328 · Sep 26, 2018 at 06:11 PM 0
Share

Oh, looking at your explanation of why things aren't working for Coder333's answer, you're loading a new scene when the player dies it looks like, right? Well, then the deathCount variable isn't going to "remember" the previous deaths! So I think that's your issue. You can do two things here: use PlayerPrefs to save the deathCount after each death like this:

 // Increase death count by 1
 int newDeathCount = PlayerPrefs.GetInt("PlayerDeathCount", 0) + 1;
 PlayerPrefs.SetInt("PlayerDeathCount", newDeathCount);
 
 if (newDeathCount % 4 == 0) {
     //...
 } 

You also can make the deathCount variable static, so it isn't specific to instances of your player script.

avatar image bennett_apps Stratosome · Sep 26, 2018 at 07:47 PM 0
Share

@Stratosome you're totally right, looking back, happy you found the answer but a bit bummed i couldn't find that myself! Yay, but dang it (:(

Upvoting ur answer now. @hanaan328 you've found ur answer, mark this as correct, too bad my answer wasn't quite there.

Show more comments
avatar image hanaan328 · Sep 27, 2018 at 02:09 AM 0
Share

Just changed the counter to static and it's working now! Thanks so much for all of your guys' replies and help, much appreciated :)

avatar image Stratosome hanaan328 · Sep 27, 2018 at 02:15 AM 0
Share

Woot, glad it worked for ya.

avatar image
2

Answer by bennett_apps · Sep 26, 2018 at 03:45 PM

  int counter = 0;
 
  private void OnTriggerEnter2D(Collider2D collision
  {
          if (collision.tag == "Obstacle")
          {
              counter++;
              if (counter == 4) {
                  counter = 0;
                  showAd();
              }
          }
  }
  
   private void showAd()
      {
          if (Chartboost.hasInterstitial(CBLocation.Default))
          {
              Chartboost.showInterstitial(CBLocation.Default);
          }
          else
          {
              Chartboost.cacheInterstitial(CBLocation.Default);
          }
      }
Comment
Add comment · Show 6 · 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 hanaan328 · Sep 26, 2018 at 04:02 PM 0
Share

Just tried this, doesn't work :/ I added a Debug.Log to see if it worked but nothing appeared in my console.

avatar image bennett_apps hanaan328 · Sep 26, 2018 at 04:43 PM 0
Share

I would assume you do something other than just show an ad when the player dies? like...end the game, pop up a restart, reload the scene, anything? Also, when you say "didn't work", what did it do? show the ad every time?

avatar image hanaan328 bennett_apps · Sep 26, 2018 at 04:55 PM 0
Share

Yeah, when the player dies, multiple things happen. Here it is:

 if (collision.tag == "Obstacle")
         {
             Instantiate(particles, transform.position, Quaternion.identity);
             StartCoroutine(DelayLoad());
             gameObject.GetComponent<SpriteRenderer>().enabled = false;
             counter++;
             if (counter == 4)
             {
                 counter = 0;
                 showAd();
             }
         }

This is what's in the coroutine:

 Scene$$anonymous$$anager.LoadScene("$$anonymous$$enu");
         scoreText.text = 0.ToString();
         score = 0;

And as for what happens, the ad doesn't even load once

Show more comments
avatar image
1

Answer by Casiell · Sep 26, 2018 at 03:36 PM

Keep an integer counter on some object that you never destroy, or just make the counter static.

Increase counter by 1 every time your player dies

When counter is greater or equal to 4, reset counter and show an ad

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 hanaan328 · Sep 26, 2018 at 03:47 PM 0
Share

I'm new to C#, could you tell me how to make a counter?

avatar image Casiell hanaan328 · Sep 26, 2018 at 08:34 PM 0
Share

A 'counter' is not a C# structure or anything. It's an idea of something that you use to record a count of something.

Also I told you: create an integer, like a regular field you declare at the top of your class. Start from zero and increase it every time your player dies.

Looking at other answers that explain the exact same thing, I see that you have a different problem: you reload the scene causing you to lose all data that was stored in any object on that scene.

From what I gathered, it can be avoided by making your integer static. So just take the code from Coder333 and write 'static' before 'int counter = 0;'

You can also use Stratosome code if it works, but I don't really like it and I will explain why:

  1. He doesn't reset the counter meaning that at some point it can reach values bigger than integer max value (2,147,483,648). It's really unlikely, but let's not learn bad practices when we can just set it back to zero with no effort.

  2. PlayerPrefs.SetInt writes the value to device registry. This has a benefit of keeping your values between games (like when you restart the game). But writing to registry is a REALLY BAD thing to do, especially for such unimportant things. One value is like whatever, but in final application you may have dozens of similar cases and each will have it's own registry key. Just no, if you want to save something between sessions, make yourself a save file.

I guess I should've reversed the order of my points, because second is really important. Avoid saving to registry when possible, just don't do that, there will be a time when you know exactly what you are doing, but for now, please avoid that.

avatar image
1

Answer by Omti1990 · Sep 26, 2018 at 09:34 PM

So having seen the other comments, it seems you're loading a new scene every time the player dies. As a result you need something that saves your data when you switch scenes. (Normally data just gets deleted when you load a new scene)

The best option imho would be to create a static class that saves all the game data you want to transfer between scenes. Like this: (You see this class doesn't inherit from Monobehavior, so if you create a new script you need to remove the ": Monobehavior"

 public static class StaticGameObjects
 {
 
     public static int deathCounter = 0;
 }
 

Then you'd change your original code like this:

 private void OnTriggerEnter2D(Collider2D collision)
     {
         if (collision.tag == "Obstacle")
         {
             //Increases the death counter every time the player dies.
             StaticGameObjects.deathCounter++;
 
             //In case that the death Counter reaches 4 show the add and reset the deathCounter
             if (StaticGameObjects.deathCounter == 4)
             {
                 showAd();
                 StaticGameObjects.deathCounter = 0;
             }
             
         }
  }
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 SpawnofTarterus · Sep 26, 2018 at 03:36 PM

I'm just learning how to code now so you are a bit far ahead of me, but couldn't you just set a variable to count up each time the play dies? Once it hit's four it will trigger the ad to play and then reset itself back to 0.

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 hanaan328 · Sep 26, 2018 at 03:42 PM 0
Share

Afraid I'm new to C# too, how would I make a variable counter?

avatar image SpawnofTarterus hanaan328 · Sep 26, 2018 at 03:50 PM 0
Share

So like Casiell said, first pick a script linked to a gameObject in your game that you never destroy. On that script do the following:

int deathCounter = 0;

Then in your method that kills the player put:

deathCounter++;

Then make a method to play the ad that only triggers if deathCounter is >= 4 In this same method when it plays the ad also add a line that resets deathCounter to 0. deathCounter = 0;

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

557 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Chartboost adds are not showing up on android 0 Answers

How can I add a microtransaction to my game that will disable ads for 24 hours? (C#) 0 Answers

Why is Unity Ads not showing an ad in unity 5.2? 5 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