Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Trying to show ads at a certain score.

Discussion in 'Scripting' started by oyunatarco, Jul 26, 2020.

  1. oyunatarco

    oyunatarco

    Joined:
    Jul 23, 2020
    Posts:
    18
    Hello there I wrote this to show ads when the player gets to score 3000 however it is not working can you give me a hint on what im doing wrong?

    Code (CSharp):
    1.     public void AddScore(int n)
    2.     {
    3.         currentScore += n;
    4.         adScore += n;
    5.         text_score.text = currentScore.ToString();
    6.         if (adScore >= 3000)
    7.         {
    8.             GetComponent<AdsMan>();
    9.             adScore = 0;
    10.         }
    11.     }
    And here is AdsMan:

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.Advertisements;
    6.  
    7. public class AdsMan : MonoBehaviour
    8. {
    9.     IEnumerator Start()
    10.     {
    11.         Advertisement.Initialize("-------", true); //dont mind the -'s its correct in the code
    12.         while (!Advertisement.IsReady())
    13.         {
    14.             yield return null;
    15.         }
    16.             Advertisement.Show();
    17.     }
    18. }
     
    Last edited: Jul 26, 2020
  2. UltraShards

    UltraShards

    Joined:
    Jul 3, 2020
    Posts:
    7
    With your code you're only getting the AdsMan script, not showing the ad with it.
    Try this:
    Code (CSharp):
    1. GetComponenet<AdsMan>().Start();
    This will run the Start() function when it's called.
     
  3. oyunatarco

    oyunatarco

    Joined:
    Jul 23, 2020
    Posts:
    18
    Oh yeah right! it seems its not only that but i also forgot to add public to IEnumerator. Although its still not working.
     
  4. speedyfast734

    speedyfast734

    Joined:
    Jun 21, 2015
    Posts:
    9
    U need this to start thr Coroutine

    StartCoroutine(Start);
     
    oyunatarco likes this.
  5. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Start is a coroutine on a Monobehaviour, which Unity will automatically run when the Monobehaviour is created.

    I would suggest renaming the Start function to avoid this (probably) unwanted behavior (e.g. rename it to ShowAdd) and starting the coroutine manually

    Code (CSharp):
    1. StartCoroutine(GetComponent<AdsMan>().ShowAdd());
    2.