After reloading game lose connection to player Controller after ad

At first, the game works fine crash play ad to continues playing. The next crash then reloads the game after that next crash I play ad after that I lose connection to the player Controller to enable the player to continue playing. I am making an infinite Runner I get this:
MissingReferenceException: The object of type ‘PlayerController’ 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.
some of the ad code and I am getting the debug.log

public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    {
        if (showResult == ShowResult.Finished)
        {
            playerController.ResumeGame();
            Debug.Log("Ad R finished");
        }
        else if(showResult == ShowResult.Failed)
        {
            gameManager.reSume = true;
            Debug.Log("Ad R Failed");
        }
    }

what I am losing the part of the player controller and looking at the inspector it’s still there.

 public void ResumeGame()
    {

        Debug.Log("trying to resume");
        this.transform.position = new Vector3(0, 2f, transform.position.z);
        resetbox.SetActive(false);
        anim.Play("Warrior Idle");
        gameManager.readyToReSume = true;
        Debug.Log("Ready to keep going");
    }

Hello. Your code does not show enough information. I guess that somewhere you destroy your PlayerController or you lose its reference, maybe restarting the scene (as your title says). Inspector cannot lie - you destroyed your PlayerController :confused:
However, it’s not the best way (rush job), but you could forced the PlayerController reference adding an IF statement is the Update section:

if (player == null)
    player = GameObject.FindWithTag("Player");

Or you can keep it alive using DontDestroyOnLoad().

ya not luck on that even tryed DontDestroyOnLoad() gave me a double player

 public GameObject me;
    public GameManager gameManager;
    public Animator anim;
    public GameObject resetbox;
    public bool resetPlayer = false;

    void Awake()
    {
        me = this.gameObject;
        gameManager = GameManager.gameManager;
    }

    void Start()
    {
        anim = GetComponentInChildren<Animator>();
        resetbox.SetActive(false);
    }

    public void ResetPlayerCrahs()
    {
        resetbox.SetActive(true);
    }

    void OnTriggerEnter(Collider other)
    {
        if(other.tag == "Crash Environment")
        {
            anim.Play("Falling Down");
            gameManager.gameOver = true;
            gameManager.ReSumeedOrRestartLevel();
            gameManager.canPlay = false;
        }
    }

    public void ResumeGame()
    {
//this is also what i tryed got the same thing
        if (me != null)
        {
            Debug.Log("trying to resume");
            this.transform.position = new Vector3(0, 2f, transform.position.z);
            resetbox.SetActive(false);
            anim.Play("Warrior Idle");
            gameManager.readyToReSume = true;
            Debug.Log("Ready to keep going");
        }else if (me == null)
        {
            Debug.Log("trying to resume");
            this.transform.position = new Vector3(0, 2f, transform.position.z);
            resetbox.SetActive(false);
            anim.Play("Warrior Idle");
            gameManager.readyToReSume = true;
            Debug.Log("Ready to keep going");
        }
    }
}
and i do have GameObject.FindGameObjectWithTag in start
 IEnumerator Start()
    {
        errorTextObj.SetActive(false);
        errorText.text = " ";
        gameManager = GameManager.gameManager;
        playerController = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
#if UNITY_IOS
        Advertisement.Initialize(myGameIdIOS, testMode);
#else
        Advertisement.Initialize(myGameIdAndroid, testMode);
#endif
        Advertisement.AddListener(this);
        Debug.Log("Getting Ad R Ready");
        while (!Advertisement.IsReady(placement))
        {
            adButton.interactable = false;
            yield return null;
        }
    }

May I take a look at the GameManager script, especially the ReSumeedOrRestartLevel() function ? I am pretty sure that you did something wrong here. Respectfully, your code is a little bit hard coded. In the ResumeGame function, IF and ELSEIF have the same code !!! Just a little advice ; check in all your scripts where there is a Destroy() calling. There is something wrong somewhere ++

hi ok i looked over every code i have nothing is calling Destroy() for playercontroller.
here is just ReSumeedOrRestartLevel() thanks for taking the time to help out.

public void ReSumeedOrRestartLevel() 
    {
        generatelevel.enabled = false;
        playerMove.enabled = false;
        reSumeButton.SetActive(false);
        if (resumed == false)
        {
            adButton.SetActive(true);
            reStartButten.SetActive(true);
            Debug.Log("Can Be ReSumeed");
        }else if (resumed == true)
        {
            adButton.SetActive(false);
            reStartButten.SetActive(true);
            Debug.Log("already ReSumeed");
        }

here is the hole GameManager

 public static GameManager gameManager;

    [Header("Player Code/stuff")]
    public PlayerMove playerMove;
    public Animator anim;
    public GameObject reSetBox;
    public GameObject player;

    [Header("Game Stuff")]
    public bool gameOver = false;
    public Generatelevel generatelevel;
    public bool resumed = false;
    public bool readyToResume = false;
    public bool canPlay = true;

    [Header("Menu Stuff")]
    public GameObject pauseMenu;
    public GameObject musicManager;
    public Slider slider;
    public AudioSource audioS;
    public GameObject reStartButten;
    public bool isPause = false;
    public GameObject playButton;
    public GameObject reSumeButton;
    public GameObject adButton;
    public TMP_Text errorText;
    public GameObject errorTextObj;

    [Header("Ads Code")]
    public AdManager adManager;
    public RewardedAdDisplay rewardedAd;
    public bool showedAd = false;

    void Awake()
    {
        if (gameManager != null && gameManager != this)
        {
            Destroy(this);
            Debug.Log("More then One GameManager");
        }
        else
        {
            gameManager = this;
            Debug.Log("Game Manager set");
        }

        playerMove = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMove>();
        rewardedAd = GameObject.FindGameObjectWithTag("RewardedAd").GetComponent<RewardedAdDisplay>();
    }

    void Start()
    {
        reSetBox.SetActive(false);
        generatelevel.enabled = false;
        playerMove.enabled = false;
        reStartButten.SetActive(false);
        pauseMenu.SetActive(false);
        musicManager.SetActive(true);
        slider.interactable = true;
        slider.value = audioS.volume;
        playButton.SetActive(true);
        reSumeButton.SetActive(false);
        adButton.SetActive(false);
    }

    void Update()
    {
        if(readyToResume == true)
        {
            reSumeButton.SetActive(true);
            Debug.Log("ready to resume " + readyToResume);
        }

        if (isPause == true && canPlay == true)
        {
            anim.enabled = false;
            generatelevel.enabled = false;
            playerMove.isPause = isPause;
        }
        else if (isPause == false && canPlay == true)
        {
            anim.enabled = true;
            generatelevel.enabled = true;
            playerMove.isPause = isPause;
        }
    }

    public void PlayGameButton()
    {
        gameOver = false;
        generatelevel.enabled = true;
        anim.Play("Run");
        playerMove.enabled = true;
        playButton.SetActive(false);
    }

    public void ReSumeedOrRestartLevel()
    {
        generatelevel.enabled = false;
        playerMove.enabled = false;
        reSumeButton.SetActive(false);
        if (resumed == false)
        {
            adButton.SetActive(true);
            reStartButten.SetActive(true);
            Debug.Log("Can Be ReSumeed");
        }else if (resumed == true)
        {
            adButton.SetActive(false);
            reStartButten.SetActive(true);
            Debug.Log("already ReSumeed");
        }
    }
  
    public void ReSumeButton()
    {
        readyToResume = false;
        playerMove.enabled = true;
        anim.Play("Run");
        gameOver = false;
        generatelevel.enabled = true;
        reSumeButton.SetActive(false);
        reStartButten.SetActive(false);
        adButton.SetActive(false);
        resumed = true;
        canPlay = true;
        Debug.Log("ReSumed button");
    }

    public void ReStartLevelButton()
    {
        SceneManager.LoadScene(0);
    }

    public void PauseUnPause()
    {
        isPause = !isPause;
        pauseMenu.SetActive(!pauseMenu.activeSelf);
        Debug.Log(isPause);
    }

   public void SetVolme()
    {
        audioS.volume = slider.value;
    }

    public void MusicON_Off()
    {
        musicManager.SetActive(!musicManager.activeSelf);
        slider.interactable = !slider.interactable;
    }

    public void QuitErrorText()
    {
        errorText.text = " ";
        errorTextObj.SetActive(false);
    }

    public void Quit()
    {
        Application.Quit();
    }

talking about the ResumeGame() if else if me thing I just added that to see what would happen normally I have it this way.

public void ResumeGame()
  {
            Debug.Log("trying to resume");
            this.transform.position = new Vector3(0, 2f, transform.position.z);
            resetbox.SetActive(false);
            anim.Play("Warrior Idle");
            gameManager.readyToReSume = true;
            Debug.Log("Ready to keep going");
       
    }
}

Nothing. I regret, but I cannot identify your bug. It’s really hard coded, but in the same time, it seems correct. In your inspector, you have a long message with the error (as you said, player does not exist anymore), but you have some numbers too and scripts name. It could help you to find the problem ++

I’m not sure what you mean but here is what im looking at

7414772--906947--bug 3.jpg

ita something to do with ad script. I added hearts to use instead of watching ads and it works just fine. I change the ad script to go through game manager first and it still gives me the same thing.

Here is the ad code idk what is happening.

 IEnumerator Start()
    {
        errorTextObj.SetActive(false);
        errorText.text = " ";
        gameManager = GameManager.gameManager;
        playerController = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
#if UNITY_IOS
        Advertisement.Initialize(myGameIdIOS, testMode);
#else
        Advertisement.Initialize(myGameIdAndroid, testMode);
#endif
        Advertisement.AddListener(this);
        Debug.Log("Getting Ad R Ready");
        while (!Advertisement.IsReady(placement))
        {
            adButton.interactable = false;
            yield return null;
        }
    }

    public void OnUnityAdsReady(string placementId)
    {
        adButton.interactable = true;
        Debug.Log("Ad R Ready");
    }

    public void ShowAd(string p)
    {
        Advertisement.Show(p);
        playerController.ResetPlayerCrahs();
        Debug.Log("Showing Ad R");
        gameManager.adButton.SetActive(false);
        gameManager.heartButton.SetActive(false);
    }

    public void OnUnityAdsDidStart(string placementId)
    {

    }

    public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
    {
        if (showResult == ShowResult.Finished)
        {
            //using going through playercontroller or the gamemanager still gives me the samething
            //playerController.ResumeGame();
            gameManager.TestOnly();
            Debug.Log("Ad R finished");
        }
        else if(showResult == ShowResult.Failed)
        {
            gameManager.resumed = true;
            Debug.Log("Ad R Failed");
        }
    }

The message in the inspector is clear. Somewhere in your scripts, during the process, you disable (or destroy) the PlayerController script. I guess that it happens in the RewardedAdDisplay script (maybe line 67). Sorry, but I cannot do more ++

Hi, I Fixed it’s kind of a workaround. instead of the reward add playerController.ResumeGame now ad gives you heart. then use the hearts to continue

If it works - sometimes it’s enough :slight_smile: