Script Not Working After Changing Scene

I Have A Script That When I Click A Button I Get A Camera Every Second But When I Change My Scene To The Game It Stops Giving Me Cameras Every Second. Do You Know Why?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class AutoScript : MonoBehaviour
{
    public GameObject NoMoney;
    public static int PerAmount;

    void Start()
    {
        
        PerAmount = 0;
          

        

    }


    public void GiveCamera()
    {
        if (CameraAmount.CameraAmounts > 50)
        {
            PerAmount += 1;
            CameraAmount.CameraAmounts -= 50;
            InvokeRepeating("MoneyGive", 1, 1f);
        }
        else
        {
            NoMoney.SetActive(true);
            InvokeRepeating("NoMoneys", 1, 1f);
            
            
        }
    
    }
    public void NoMoneys()
    {
        NoMoney.SetActive(false);
    }
    public void MoneyGive()
    {
        CameraAmount.CameraAmounts += PurchaseAutoOnClick.PurchaseClicked;
    }

Provided you have designed your game object optimally, just add this code to your script:

void Awake ()
{
    DontDestroyOnLoad (this.gameObject);
}

So what do I mean by designed optimally? If you have written your code so that the only function on this gameobject is what you want in the next scene, then you are OK. If you put code in the gameobject that should really belong elsewhere (eg managing a heath bar, writing to the UI, updating enemies etc) then you will potentially have a problem.

It’s not just this script, though. dontdestroyonload preserves everything about this gameobject - other scripts and child objects included so you have to think carefully about how to use it. It is commonly used for maintaining background music across scene changes but, with care, can be used for other things as well.

I’ve also seen my students use dontdestroyonload to keep variable values between scenes - info that they need to keep from one scene to the next. This is typically used in adventure games or where you have an inventory.

You might also want to consider turning off camera spawning when loading a scene and restarting it again after the new scene is loaded. Events would be a good way to do this to avoid code dependencies.

Is there an alternative? If this gameobject is a prefab, you could just include it in the new scene, preserving values as mentioned above or in statics or PlayerPrefs or even (in these days of fast SSDs) in a JSON file.

do i need to change this.gameobject to something or is it fine? Void Awake is down at the bottom.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class AutoScript : MonoBehaviour
{
    public GameObject NoMoney;
    public static int PerAmount;

    void Start()
    {
        
        PerAmount = 0;
          

        

    }


    public void GiveCamera()
    {
        if (CameraAmount.CameraAmounts > 50)
        {
            PerAmount += 1;
            CameraAmount.CameraAmounts -= 50;
            InvokeRepeating("MoneyGive", 1, 1f);
        }
        else
        {
            NoMoney.SetActive(true);
            InvokeRepeating("NoMoneys", 1, 1f);
            
            
        }
    
    }
    public void NoMoneys()
    {
        NoMoney.SetActive(false);
    }
    public void MoneyGive()
    {
        CameraAmount.CameraAmounts += PurchaseAutoOnClick.PurchaseClicked;
    }
    void Awake()
    {
        DontDestroyOnLoad(this.gameObject);
    }
}