G
G
gorgey2018-01-12 21:15:20
contextual advertising
gorgey, 2018-01-12 21:15:20

Error in the script, Unity ADS, how to configure?

Hello dear friends. Immediately I apologize for the noob for the question, but without your participation I will not be able to figure it out. It is necessary to fix the script, advertising is included at the beginning of the scene, but it is necessary that it be included every 5 losses. Thanks in advance and good luck to all.

using UnityEngine;
using UnityEngine.Advertisements;
using System.Collections;

public class GameState : Singleton<GameState> {

  public bool startGame = false;
  public bool endGame = false;
  private Bird player;
  public BackgroundGame[] background;

  private CoinsManager coins;

  private static int advCount = 0;

  public GameObject uiEnd;
  public GameObject uiStart;
  public GameObject uiScore;
  public GameObject uiHightCoins;
  public bool deleteSave = false;

  public AudioSource music;

  void Start () {
    if (PlayerPrefs.GetString ("NoAds") != "yes") {
      if (Advertisement.isSupported) {
        Advertisement.Initialize ("1667763", false);
      }	else
        Debug.Log ("Platform is not supported");
      if (deleteSave)
        PlayerPrefs.DeleteAll ();

      player = FindObjectOfType<Bird> ();
      coins = FindObjectOfType<CoinsManager> ();

      uiEnd.SetActive(false);
      uiStart.SetActive (true);
      uiScore.SetActive (true);

      startGame = false;
      endGame = false;
      music.Stop ();}
         }

    void Update () {
      if(PlayerPrefs.GetString ("NoAds") != "yes") {
        advCount++;
        if (Advertisement.IsReady ()&& advCount %5 == 0)
          Advertisement.Show();
        for (int i = 0; i < background.Length; i++) {
          if(!startGame)
          {
            background[i].enabled = false;
          }else{
            background[i].enabled = true;
          }
        }

        if(player.isDead)
        {
          endGame = true;
          startGame = false;
          music.Stop();
        }

        if(endGame)
        {
          if(coins.Coin > coins.MaxCoins)
          {
            coins.MaxCoins = coins.Coin;
            PlayerPrefs.SetInt("Coins", coins.MaxCoins);
            coins.MaxCoins = PlayerPrefs.GetInt("Coins");
            coins.UpdateHightCoins("<color=yellow>New </color>",coins.MaxCoins);
          }

          if(Input.GetMouseButtonDown(0))
            Application.LoadLevel(Application.loadedLevel + 0);

          uiEnd.SetActive(true);
          uiScore.SetActive(false);

        }

        if(!endGame)

          if (Input.GetMouseButtonDown (0) && !startGame)
          {
            startGame = true;
            endGame = false;
            music.Play();

            uiStart.SetActive (false);
          }
        }

           }

}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Denis Gaydak, 2018-01-12
@gorgey

to get started, paste the code through a special tag
but in essence the question ..
you can of course just write you the correct option, but let's try to LEARN ..
here are a couple of hinting questions
for you - are you aware that Update is called EVERY FRAME ?? and what will you have with advCount for the first five frames?
- where is your logic of losing? (so I see that it seems like in the place where the player.isDead flag is checked)
- didn't you think that the loss counter should be increased just at the place of the loss?
- and perhaps then in the same place where we increase the loss counter - it’s worth checking that this is the FIFTH loss in a row and launching advertising)
judging by the code, if it’s yours, of course, then checking for “every fifth” - the remainder of the division is you you can do
and the method of launching the display of advertising - you also know how to launch it.
I hope this helps you. if it’s not clear from all the hints, then I would suggest looking at some basics and simple courses in order to start developing such a kind of “algorithmic programmer” thinking))

G
gorgey, 2018-01-13
@gorgey

All solved, thank you all.

using UnityEngine;
using UnityEngine.Advertisements;
using System.Collections;

public class GameState : Singleton<GameState> {

  public bool startGame = false;
  public bool endGame = false;
  private Bird player;
  public BackgroundGame[] background;

  private CoinsManager coins;

  private static int advCount = 0;

  public GameObject uiEnd;
  public GameObject uiStart;
  public GameObject uiScore;
  public GameObject uiHightCoins;
  public bool deleteSave = false;

  public AudioSource music;

  void Start () {
    if (PlayerPrefs.GetString ("NoAds") != "yes") {
      if (Advertisement.isSupported) {
        Advertisement.Initialize ("1667763", false);
      }	else
        Debug.Log ("Platform is not supported");
      if (deleteSave)
        PlayerPrefs.DeleteAll ();

      player = FindObjectOfType<Bird> ();
      coins = FindObjectOfType<CoinsManager> ();

      uiEnd.SetActive(false);
      uiStart.SetActive (true);
      uiScore.SetActive (true);

      startGame = false;
      endGame = false;
      advCount++;
      music.Stop ();}
  }

  void Update () {
    if(PlayerPrefs.GetString ("NoAds") != "yes") {
      if (Advertisement.IsReady ()&& advCount %5 == 0)
        Advertisement.Show();
      for (int i = 0; i < background.Length; i++) {
        if(!startGame)
        {
          background[i].enabled = false;
        }else{
          background[i].enabled = true;
        }
      }

      if(player.isDead)
      {
        endGame = true;
        startGame = false;
        music.Stop();
      }

      if(endGame)
      {
        if(coins.Coin > coins.MaxCoins)
        {
          coins.MaxCoins = coins.Coin;
          PlayerPrefs.SetInt("Coins", coins.MaxCoins);
          coins.MaxCoins = PlayerPrefs.GetInt("Coins");
          coins.UpdateHightCoins("<color=yellow>New </color>",coins.MaxCoins);
        }

        if(Input.GetMouseButtonDown(0))
          Application.LoadLevel(Application.loadedLevel + 0);

        uiEnd.SetActive(true);
        uiScore.SetActive(false);

      }

      if(!endGame)

      if (Input.GetMouseButtonDown (0) && !startGame)
      {
        startGame = true;
        endGame = false;
        music.Play();

        uiStart.SetActive (false);
      }
    }

  }

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question