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. Dismiss Notice

Question loading previous scene

Discussion in 'Scripting' started by Digital_Owl, Apr 30, 2022.

  1. Digital_Owl

    Digital_Owl

    Joined:
    Feb 7, 2021
    Posts:
    46
    i want my rewarded video ad to grant a player a new life, meaning i want it to load the previous scene and continue playing from there instead of returning at the very beginning, but when my player dies it transport to the lose scene, how do i make the button so when player presses it video ad plays and when video is done it loads previous scene, the scene it died to.

    ive set up rewarded ads script i'm just confused what to put in the //grant a reward graph. PLEASE HELP and of course thank you!

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.Advertisements;
    4.  
    5. public class RewardedAdsButton : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
    6. {
    7.     [SerializeField] Button _showAdButton;
    8.     [SerializeField] string _androidAdUnitId = "Rewarded_Android";
    9.     [SerializeField] string _iOSAdUnitId = "Rewarded_iOS";
    10.     string _adUnitId = null; // This will remain null for unsupported platforms
    11.  
    12.     void Awake()
    13.     {
    14.         // Get the Ad Unit ID for the current platform:
    15. #if UNITY_IOS
    16.         _adUnitId = _iOSAdUnitId;
    17. #elif UNITY_ANDROID
    18.         _adUnitId = _androidAdUnitId;
    19. #endif
    20.  
    21.         //Disable the button until the ad is ready to show:
    22.         _showAdButton.interactable = false;
    23.     }
    24.  
    25.     // Load content to the Ad Unit:
    26.     public void LoadAd()
    27.     {
    28.         // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).
    29.         Debug.Log("Loading Ad: " + _adUnitId);
    30.         Advertisement.Load(_adUnitId, this);
    31.     }
    32.  
    33.     // If the ad successfully loads, add a listener to the button and enable it:
    34.     public void OnUnityAdsAdLoaded(string adUnitId)
    35.     {
    36.         Debug.Log("Ad Loaded: " + adUnitId);
    37.  
    38.         if (adUnitId.Equals(_adUnitId))
    39.         {
    40.             // Configure the button to call the ShowAd() method when clicked:
    41.             _showAdButton.onClick.AddListener(ShowAd);
    42.             // Enable the button for users to click:
    43.             _showAdButton.interactable = true;
    44.         }
    45.     }
    46.  
    47.     // Implement a method to execute when the user clicks the button:
    48.     public void ShowAd()
    49.     {
    50.         // Disable the button:
    51.         _showAdButton.interactable = false;
    52.         // Then show the ad:
    53.         Advertisement.Show(_adUnitId, this);
    54.     }
    55.  
    56.     // Implement the Show Listener's OnUnityAdsShowComplete callback method to determine if the user gets a reward:
    57.     public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState)
    58.     {
    59.         if (adUnitId.Equals(_adUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED))
    60.         {
    61.             Debug.Log("Unity Ads Rewarded Ad Completed");
    62.             // Grant a reward.
    63.  
    64.             // Load another ad:
    65.             Advertisement.Load(_adUnitId, this);
    66.         }
    67.     }
    68.  
    69.     // Implement Load and Show Listener error callbacks:
    70.     public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message)
    71.     {
    72.         Debug.Log($"Error loading Ad Unit {adUnitId}: {error.ToString()} - {message}");
    73.         // Use the error details to determine whether to try to load another ad.
    74.     }
    75.  
    76.     public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message)
    77.     {
    78.         Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}");
    79.         // Use the error details to determine whether to try to load another ad.
    80.     }
    81.  
    82.     public void OnUnityAdsShowStart(string adUnitId) { }
    83.     public void OnUnityAdsShowClick(string adUnitId) { }
    84.  
    85.     void OnDestroy()
    86.     {
    87.         // Clean up the button listeners:
    88.         _showAdButton.onClick.RemoveAllListeners();
    89.     }
    90. }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    My suggestion would be to possibly just load the lose scene as additive and have it setup to be over the level scene. Or, your lose could be a popup or something that you turn on or off within the level scene.

    Otherwise, depending on how you have it setup, you're going to need either static variables or a floating object that tracks what scene you were on, what position your player was at, what was alive, collected, interacted with, etc that you need to make sure is all reset.

    Then, when you load the level scene, you'll need to access that data and reset it all... which is why, loading the lose scene as additive and just turning off certain things in the level scene might be easier to handle.

    You're just going to have to figure out what you need to track before your "you lose" is loaded in if you want to switch scenes. Then when they watch the ad, you check if there is a level in progress through that data and setup the scene again.
     
  3. Digital_Owl

    Digital_Owl

    Joined:
    Feb 7, 2021
    Posts:
    46

    i don't have any point, money or anything like that in the game, no data to be collected, i literally only want to load previous scene, nothing else, idea about pop up is good tho, i thought that myself but i really want to make this scene work, thanks for answer!
     
  4. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,108
    It's not like you have much choice here, Brathnann actually told you all possible solutions. If you have scene with some internal state and then you unload it, you lose this data.
    You have two options to get it back - you don't do it (unload) and you keep this scene in memory or you need to keep some variables to be able to recreate this scene again.
     
    Kurt-Dekker likes this.
  5. Digital_Owl

    Digital_Owl

    Joined:
    Feb 7, 2021
    Posts:
    46
    how do i store last scene in the variable?
     
  6. Qriva

    Qriva

    Joined:
    Jun 30, 2019
    Posts:
    1,108
    I don't think it's possible, or rather it does not work this way - as he said you would need to use additive scene.
     
  7. Digital_Owl

    Digital_Owl

    Joined:
    Feb 7, 2021
    Posts:
    46
    i used the method u guys suggested about pop up defeat screen and it works perfectly, THANKS! (but i also have new problem and ill be glad if you check it out xd)
     
  8. Digital_Owl

    Digital_Owl

    Joined:
    Feb 7, 2021
    Posts:
    46
    i used the method u guys suggested about pop up lose screen and it works pretty well THANKS! (but i also have a new problem and ill be glad if you check it out xd)
     
  9. Digital_Owl

    Digital_Owl

    Joined:
    Feb 7, 2021
    Posts:
    46
    okay my bad i just fixed it
     
  10. Digital_Owl

    Digital_Owl

    Joined:
    Feb 7, 2021
    Posts:
    46
    i fixed it