I had implemented unity ads with adButton. Now I function it in a way that after using adButton you can’t use it more and you have to restart the game and after restarting your game will restart with basic conditions. But after restart then adButton should also available for one time use as by the condtion. But as I use that adButton the game will not resume by StartCoroutine(StartGame(1.0f)). It log the following error :
`MissingReferenceException: The object of type ‘App_Initialize’ has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
**App_Initialize.cs**
void Awake(){
Shader.SetGlobalFloat("_Curvature", 2.0f);
Shader.SetGlobalFloat("_Trimming", 0.1f);
Application.targetFrameRate = 60;
}
// Start is called before the first frame update
void Start()
{
player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePosition;
inMenuUI.gameObject.SetActive(true);
inGameUI.gameObject.SetActive(false);
gameOverUI.gameObject.SetActive(false);
}
public void PlayButton(){
if (hasGameStarted == true){
StartCoroutine(StartGame(1.0f));
}
else{
StartCoroutine(StartGame(0.0f));
}
}
public void PauseButton(){
player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePosition;
hasGameStarted = true;
inMenuUI.gameObject.SetActive(true);
inGameUI.gameObject.SetActive(false);
gameOverUI.gameObject.SetActive(false);
}
public void GameOver(){
player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePosition;
hasGameStarted = true;
inMenuUI.gameObject.SetActive(false);
inGameUI.gameObject.SetActive(false);
gameOverUI.gameObject.SetActive(true);
if (hasSeen == true && restartButton!=null){
adButton.GetComponent<Image>().color = new Color(1, 1, 1, 0.5f);
adButton.GetComponent<Button>().enabled = false;
adButton.GetComponent<Animator>().enabled = false;
restartButton.GetComponent<Animator>().enabled = true;
}
}
public void RestartGame(){
SceneManager.LoadScene(0); //Start scene from index inside LoadScene(0)
// Application.LoadLevel(Application.loadedLevel);
}
public void RewardSeen(){
hasSeen = true;
StartCoroutine(StartGame(1.0f));
}
IEnumerator StartGame(float waitTime){
inMenuUI.gameObject.SetActive(false);
inGameUI.gameObject.SetActive(true);
gameOverUI.gameObject.SetActive(false);
yield return new WaitForSeconds(waitTime);
player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePositionY;
}
}
**RewardedAdsButton.cs**
#if UNITY_IOS
private string gameId = "placeholder";
#elif UNITY_ANDROID
private string gameId = "placeholder";
#endif
Button myButton;
public string myPlacementId = "rewardedVideo";
void Start () {
myButton = GetComponent <Button> ();
// Set interactivity to be dependent on the Placement’s status:
myButton.interactable = Advertisement.IsReady (myPlacementId);
// Map the ShowRewardedVideo function to the button’s click listener:
if (myButton) myButton.onClick.AddListener (ShowRewardedVideo);
// Initialize the Ads listener and service:
Advertisement.AddListener (this);
Advertisement.Initialize (gameId, true);
}
// Implement a function for showing a rewarded video ad:
void ShowRewardedVideo () {
Advertisement.Show (myPlacementId);
}
// Implement IUnityAdsListener interface methods:
public void OnUnityAdsReady (string placementId) {
// If the ready Placement is rewarded, activate the button:
if (placementId == myPlacementId) {
myButton.interactable = true;
}
}
public void OnUnityAdsDidFinish (string placementId, ShowResult showResult) {
// Define conditional logic for each ad completion status:
if (showResult == ShowResult.Finished) {
// StartCoroutine(StartGame(1.0f));
seeenAd.RewardSeen();
} else if (showResult == ShowResult.Skipped) {
// Do not reward the user for skipping the ad.
} else if (showResult == ShowResult.Failed) {
Debug.LogWarning ("The ad did not finish due to an error.");
}
}
public void OnUnityAdsDidError (string message) {
// Log the error.
}
public void OnUnityAdsDidStart (string placementId) {
// Optional actions to take when the end-users triggers an ad.
}
}
`