Unity method only runs first line of code

Viewed 401

I've AdMob test ads fully working in my project. When the ad is watched the below code should run, however only the first line is being executed, the 2 other lines are being ignored with no error.

public void HandleUserEarnedReward(object sender, Reward args)
{
    GameControl.control.life += 1;
    GameControl.control.Save();
    txtLife.text = GameControl.control.life.ToString();
}

I've placed the same code in another method that I trigger with a button to test. In this instance the full method is run.

public void runtest()
{      
    GameControl.control.life += 1;
    GameControl.control.Save();
    txtLife.text = GameControl.control.life.ToString();
}
1 Answers

If i'm not wrong you are trying to give reward after User watches rewarded video. I had the same problem one time, the problem is that functions that are executing inside of "HandleUserEarnedReward" will NOT be executed on unity's MainThread, but on Google's SDK thread. There are several solutions:

  1. https://github.com/PimDeWitte/UnityMainThreadDispatcher - Switch to main thread using this. Check readme file to more info.

  2. Create global booleans with false value. Then on "HandleUserEarnedReward" change "isRewarded" boolean to true. Create Update function to check boolean value. Something like:

     void Update()
    {
         if (isRewarded)
         {
             // do all the actions
             // reward the player
    
             isRewarded = false; // to make sure this action will happen only once.
         }
    }
    
  1. Use Coroutine. Coroutines autoMagically switch to Unity's MainThread after "yield return"

     public void HandleRewardBasedVideoRewarded(object sender, Reward args)
     {
     string type = args.Type;
     double amount = args.Amount;
     Debug.Log(
         "HandleRewardBasedVideoRewarded event received for "
         + amount.ToString() + " " + type);
    
       StartCoroutine(AfunctionName());
    
     }
    
    
     IEnumerator AfunctionName()
     {
      yield return new WaitForSecondsRealtime(0.1f);
    
      // FB.LogAppEvent("AdmobRewardedView");
         Debug.Log("Reward Function Called!!!!!");
         GiveReward();
    
         this.RequestRewardBasedVideo();
     }
    
Related