- Home /
Same scripts don't work the same way on different objects
Hello everyone.
I'm trying to build a script whenever you die 5 times, the ad will appear, sound of the game will get muted, and whenever player is done with watching the ad and skips it, sound of the game will get unmuted. Alright, I achieved that, in a way. But there are only two way of dying in my game, one by falling (collision by deathline) and the other one by collision by enemy. My script with the former works great, when I apply my same script to the latter, it doesn't work. I'll share the code and then explain a bit more.
TLDR: the game sound is off during ad display while deathcount is incremented by deathfall and thus displaying ad(success), the game sound is still on in ad screen when deathcount is incremented by death by enemy and thus displaying ad, when it's supposed to be muted during ad(failure)
I'll add comments near codes to make it less complicated. I'd appreciate any kind of suggestions and helps as I am still at the very beginning of my learning and I am aware that I might have made things more complicated than they are supposed to be, as a result of my limited knowledge.
public class AdManager : MonoBehaviour
{
public string GameID = "blablabla";
public string InterstitialPlacementID = "blablabla";
public bool testMode = true;
private bool showInterstitial= false;
public static int deathCount = 5; // This is for ensuring the display of interstitial ad when you die for the first time as threshold is 6, after that it will be shown after 5 times.
public GameObject DeathLine; // so as I said I attach my script to 2 different objects. when I add my script to deathline, deathcount is incremented by player's collision with deathline after falling. when I attach this script to gameobject player, I attach my monster(enemy) to this part (deathline) in inspector so deathcount is incremented by player's collision with enemy.. think of it as deathline and enemy, the only killer objects.
public GameObject player;
bool toggle = true; //sound is on.
public GameObject deathScreen; // the screen that appears when you die.
void Start()
{
Advertisement.Initialize(GameID, testMode);
}
void Update()
{
OnDead();
if (showInterstitial)
{
if (Advertisement.IsReady(InterstitialPlacementID))
{
Advertisement.Show(InterstitialPlacementID);
deathCount++;
showInterstitial= false;
}
}
if (!deathScreen.activeInHierarchy) // when the ad was over, the game sound was still muted, so i kinda found a solution, after ad is over and you click on replay, deathScreen is hidden in hierarchy and you get the sound back.
{
toggle = true;
ToggleSound();
}
}
public void OnDead()
{
if (deathCount == 0)
{
showInterstitial= true;
toggle = false;
ToggleSound();
}
else if (deathCount == 6)
{
deathCount = 0;
}
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
deathCount++;
}
if (collision.gameObject.tag == "Enemy")
{
deathCount++;
}
}
public void ToggleSound()
{
if (toggle)
{
AudioListener.volume = 1f;
}
else
{
AudioListener.volume = 0f;
}
}
}
Your answer

Follow this Question
Related Questions
How can I spawn different GameObjects using IEnumerator? 1 Answer
Why The player don't move? 0 Answers
Newbie Spawner C# Help 3 Answers