1
votes

I was testing out some code for my game: trying to make an object already picked up to not spawn again when reloading the level. For some reason, I get this error now and I don't seem to find a solution yet (saw many had this issues while googling but none of the fixes seemed to fit)

Didn't try much as it is quite personnal. One fix wouldn't work to me as I cannot find out where to put it exactly (due to different scripts names..)

public class GameManager_GameLevel : MonoBehaviour
{
    //MANAGER
    private const int COIN_SCORE_AMOUNT = 5;
    public static GameManager_GameLevel Instance { set; get; }
    public bool isDead { set; get; }
    private bool isGameStarted = false;
    private PlayerMotor_GameLevel motor;
    public Text coinText;
    private float coinScore;
    private bool gameStarted;
    public GameObject player;
    public GameObject cameraFollow;
    private Vector3 deathPosition = new Vector3(0, 0, 30);
    public Button buttonReplay;

    //FADE
    private CanvasGroup fadeGroup;
    private float fadeInDuration = 2.0f;
    //ANIMATOR
    public Animator deathMenuAnim;
    public Animator pauseMenuAnim;
    public Animator playerAnim;


    private void Awake()
    {
        Instance = this;
        motor = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMotor_GameLevel>();
        coinText.text = coinScore.ToString("0");
    }

    private void Start()
    {
       SceneManager.LoadScene(SaveManager.Instance.currentLevel.ToString(), LoadSceneMode.Additive);

        fadeGroup = FindObjectOfType<CanvasGroup>();
        fadeGroup.alpha = 1;
    }

    public void Update()
    {
       if(Time.timeSinceLevelLoad <= fadeInDuration)
        {
            fadeGroup.alpha = 1 - (Time.timeSinceLevelLoad / fadeInDuration);
        }
        else if(!gameStarted)
        {
            fadeGroup.alpha = 0;
            gameStarted = true;
        }

        GameStarted();
    }

    public void GameStarted()
    {
        if(MobileInput_GameLevel.Instance.Tap && !isGameStarted)
        {
            isGameStarted = true;
            motor.StartRunning();
            FindObjectOfType<DecorSpawner_GameLevel>().isScrolling = true;
            FindObjectOfType<CameraMotor_GameLevel>().isMoving = true;
        }
    }

    public void GetCoin()
    {
        coinScore ++;
        coinText.text = coinScore.ToString("0");
    }

    public void OnDeath()
    {
        isDead = true;
        FindObjectOfType<DecorSpawner_GameLevel>().isScrolling = false;
        deathMenuAnim.SetTrigger("Dead");
    }

    public void Pause()
    {
        Time.timeScale = 0f;
        pauseMenuAnim.SetBool("Pause", true);
    }

    public void ResumeGame()
    {
        Time.timeScale = 1f;
        pauseMenuAnim.SetBool("Pause", false);
    }

    public void RetryButton()
    {
        Time.timeScale = 1f;
        SceneManager.LoadScene("GameLevel");
    }
    public void QuitButton()
    {
        Time.timeScale = 1f;
        SceneManager.LoadScene("MainMenu");
    }

    public void ReplayButton()
    {
        buttonReplay.gameObject.SetActive(false);
        motor.StartRunning();
        FindObjectOfType<DecorSpawner_GameLevel>().isScrolling = true;
        FindObjectOfType<CameraMotor_GameLevel>().isMoving = true;
    }
    public void Respawn()
    {
        AdController.Instance.PlayRewardedVideo();
        buttonReplay.gameObject.SetActive(true);
        player.transform.position = player.transform.position - deathPosition;
        cameraFollow.transform.position = cameraFollow.transform.position - deathPosition;
        playerAnim.SetTrigger("Reset");
        deathMenuAnim.SetTrigger("Reset");
    }
}

That issue is occurring on this script, but also on others. It actually occurs on any script getting called when I click a button in game, such as "restart, menu, option" after I already started playing. Everything is fine until calling a second scene actually.

Oh also, I don't actually destroy anything and everything was fine before. That error started out of nowhere and I didn't even change anything on all these scripts.

Edit: After someone pointed out my previous title was confusing. The object type could be CanvasGroup (the first that occured) but I also saw Text, and others.

1
your problem is that # isn't a valid name for a c# class ;) What does the exception say, verbatim? - Ruzihm
Oh, my bad. That was quite confusing I didn't realize. I said "Object #" because it's actually many of them. It could be text, gameobject, canvasgroup.. I think I saw almost everything spawning in that error. - Kazyume

1 Answers

1
votes

Apply this to the GameObject which you want to persist between scene .

refs:

var gameObject = //whatever
DontDestroyOnLoad(gameObject);

such as:

    private void Awake()
    {         
        DontDestroyOnLoad(fadeGroup);
        Instance = this;
        motor = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerMotor_GameLevel>();
        coinText.text = coinScore.ToString("0");
    }