- Home /
Question by
MonqoStudios · Dec 04, 2018 at 07:08 PM ·
error
Game restarts when it is supposed to show ads.
Hello,
In my game the player can watch an ad to activate a skill with grants the player more coins. But when the button is pressed and the ad is supposed to start, the game restarts.
My game isn't published in the playstore, could this also cause this error?
Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine.Advertisements;
using UnityEngine;
public class MainAdScript : MonoBehaviour {
public bool SkillActive = false;
public float EndCounter;
public float EndToShow;
public float StartAdCounter;
public GUIStyle TextGuiStyle;
void Update () {
if (SkillActive == true)
{
PinScript.SkillActiveBonus = 4;
}
else {
PinScript.SkillActiveBonus = 1;
}
StartAdCounter += 1 * Time.deltaTime;
if (StartAdCounter < 200) {
EndCounter = 0;
EndToShow = 60;
}
if (EndToShow < 0) {
StartAdCounter = 0;
}
}
public void ShowAd() {
if (Advertisement.IsReady())
{
Advertisement.Show("rewardedVideo", new ShowOptions() { resultCallback = HandleAdResult });
}
}
private void HandleAdResult(ShowResult result) {
switch (result) {
case ShowResult.Finished:
Debug.Log("Add Bonus");
break;
case ShowResult.Skipped:
Debug.Log("Skipped");
break;
case ShowResult.Failed:
SkillActive = true;
break;
}
}
private void OnGUI()
{
if (StartAdCounter >= 200)
{
if (SpawnBall.BallActive == false)
{
EndCounter += 1 * Time.deltaTime;
if (EndCounter > 1) {
EndCounter = 0;
EndToShow -= 1; }
GUI.Box(new Rect(Screen.width / 30, Screen.height / 5, Screen.width / 4.9f, Screen.height / 3.5f), "");
GUI.Label(new Rect(Screen.width / 18, Screen.height / 4.1f, Screen.width / 2, Screen.height / 2), "Watch an ad for 4x",TextGuiStyle);
GUI.Label(new Rect(Screen.width / 18, Screen.height / 3.6f, Screen.width / 2, Screen.height / 2), "coins for 3 minutes!", TextGuiStyle);
GUI.Label(new Rect(Screen.width / 18, Screen.height / 2, Screen.width / 2, Screen.height / 2), "Offer expires in: "+EndToShow, TextGuiStyle);
if (GUI.Button(new Rect(Screen.width / 28, Screen.height / 2.74f, Screen.width / 5, Screen.height / 8), "Sure")) {
ShowAd();
StartAdCounter = -100;
}
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Missing Component Error 1 Answer
I need help with an error 1 Answer
Every script says "can not load script" and that there is no monobehavior in the file 2 Answers
Error in the script 0 Answers
Limit rotation for a statue puzzle 1 Answer