- Home /
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);
}
}
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.
Tried this as well, having the same problem as I mentioned in Coder333's answer
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.
@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.
Just changed the counter to static and it's working now! Thanks so much for all of your guys' replies and help, much appreciated :)
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);
}
}
Just tried this, doesn't work :/ I added a Debug.Log to see if it worked but nothing appeared in my console.
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?
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
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
I'm new to C#, could you tell me how to make a counter?
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:
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.
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.
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;
}
}
}
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.
Afraid I'm new to C# too, how would I make a variable counter?
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;