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

how to add multiple reward ads in unity ads

Discussion in 'Scripting' started by max3241, Apr 23, 2020.

  1. max3241

    max3241

    Joined:
    Jul 12, 2016
    Posts:
    37
    Hey guys, I need help regarding unity ads. I implemented multiple rewards in my game. When my player dies he can respawn when he watches an ad and another one is when my player finishes playing the level, he can watch an ad to get extra coins. The problem is that when my player dies and watched an ad to respawn, he also gets the extra coins which isn't suppose to happen. He is supposed to get it after he completes the level by watches an ad. So I was wondering if there is a way to make multiple rewards appear at a specific moment.

    Code (CSharp):
    1. public bool Revive_Ad = true;
    2.  
    3. public bool Coin_Ad = true;
    4.  
    5. public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    6.  
    7. {
    8.  
    9. if (showResult == null)
    10.  
    11. {
    12.  
    13. throw new System.ArgumentNullException(nameof(showResult));
    14.  
    15. }
    16.  
    17. // Define conditional logic for each ad completion status:
    18.  
    19. if (showResult == ShowResult.Finished)
    20.  
    21. {
    22.  
    23. if (player.gameObject != null&&Revive_Ad)
    24.  
    25. {
    26.  
    27. StartTheGame();
    28.  
    29. }
    30.  
    31. if (player.gameObject != null && Revive_Ad)
    32.  
    33. {
    34.  
    35. player.GetComponent<PlayerMovement>().RespawnPlayer();
    36.  
    37. movement.enabled = true;
    38.  
    39. }
    40.  
    41. if (player.gameObject != null && Revive_Ad)
    42.  
    43. {
    44.  
    45. StartCoroutine(PlayerInmortal());
    46.  
    47. }
    48.  
    49. IEnumerator PlayerInmortal()
    50.  
    51. {
    52.  
    53. Physics.IgnoreLayerCollision(8, 9);
    54.  
    55. yield return new WaitForSeconds(2.0f);
    56.  
    57. Physics.IgnoreLayerCollision(8,9,false);
    58.  
    59. adbutton.gameObject.SetActive(false);
    60.  
    61. }
    62.  
    63. if (player.gameObject != null && Coin_Ad)
    64.  
    65. {
    66.  
    67. rewardGiven += 100;
    68.  
    69. rewardDisplayText.text = "Rewards:" + rewardGiven.ToString();
    70.  
    71. addrewardbutton.gameObject.SetActive(false);
    72.  
    73. }
    74.  
    75. }
     
    Last edited: Apr 23, 2020
  2. matkoniecz

    matkoniecz

    Joined:
    Feb 23, 2020
    Posts:
    170
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    It's a bit difficult to even attempt to read your code (see post above about code tags), but you have two bools that I noticed at the top, I'm assuming you are using these to determine what reward to give. If their default is false, then you only need to set the proper one to true and then set it back to false when you grant the reward. Note that you should also reset the bool values if the person doesn't completely watch the ad, thus not earning their reward.
     
  4. max3241

    max3241

    Joined:
    Jul 12, 2016
    Posts:
    37
    sorry for not being clear regarding the code.Is this better?
     
  5. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,385
    your code has Revive_Ad and Coin_Ad both set true. So of course it's going to do both, both are true. When you start the ad you need to flag these bools to the appropriate permutation: Revive_Ad:true, Coin_Ad:false on death, Revive_Ad:false, Coin_Ad:true on level end.

    What you could do instead of several bools like this is have an enum:
    Code (csharp):
    1. public enum AdTypes
    2. {
    3.     Revive,
    4.     Coin
    5. }
    And then a single field/variable:
    Code (csharp):
    1. public AdTypes AdType;
    And:
    Code (csharp):
    1.  
    2.     switch(AdType)
    3.     {
    4.         case AdTypes.Revive:
    5.             {
    6.                 StartCoroutine(PlayerInmortal());
    7.             }
    8.             break;
    9.         case AdTypes.Coin:
    10.             {
    11.                 rewardGiven += 100;
    12.                 rewardDisplayText.text = "Rewards:" + rewardGiven.ToString();
    13.                 addrewardbutton.gameObject.SetActive(false);
    14.             }
    15.             break;
    16.     }
    17.  
    note - not sure what the 'player.gameObject != null' is for. If the gameobject were null, then the component would be as well... that is I'm assuming player is a component. Since components MUST be attached to a gameObject.
     
    Kofiro and Yanne065 like this.
  6. SF_Games

    SF_Games

    Joined:
    Jan 17, 2020
    Posts:
    2
    Last edited: Nov 10, 2021