I am creating a 2D android game where I have 2 scene (named as MainMenu and SampleScene). I have created UI of both scenes however the issue is of missing GameObject references, when I navigate back to the main menu (By pressing pause button –> Home button) from Sample scene and then start the game again (Play button), it keeps asking to reassign the missing values. It is also showing multiple GameObjects in hierarchy with DoNotDestroy method where I can see the “missing values are shown”. Please help me figure out as I have started learning unity 2d a month ago.[[enter image description here](https://i.sstatic.net/bmmMf9vU.png)](https://i.sstatic.net/HDJ3ftOy.png)
I am attaching the code for MMElements script and PauseMenu.
public class MMElements : MonoBehaviour
{
private float previousTimeScale = 1f; // Store the previous time scale before pausing
void Awake()
{
DontDestroyOnLoad(gameObject); // Make sure this object persists across scenes
UnityEngine.Debug.Log("MMElements Awake - DontDestroyOnLoad applied");
}
public void PlayGame()
{
UnityEngine.Debug.Log("PlayGame called");
// Reset game state
Time.timeScale = previousTimeScale;
PauseMenu.isPaused = false;
// Load next scene
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
UnityEngine.Debug.Log("Scene " + (SceneManager.GetActiveScene().buildIndex + 1) + " loaded");
}
public void QuitGame()
{
FindObjectOfType<SceneMg>().ShowExitPanel();
FindObjectOfType<SceneMg>().onUserClickYesNo(1);
FindObjectOfType<SceneMg>().onUserClickYesNo(0);
// Application.Quit();
}
}
And
using UnityEngine.SceneManagement;
using UnityEngine;
public class PauseMenu : MonoBehaviour
{
public GameObject pauseMenu;
public static bool isPaused =false;
// public KeyCode pauseKey; // you can remove it
private float previousTimeScale = 1f; // Store the previous time scale before pausing
void Start()
{
pauseMenu.SetActive(false);
previousTimeScale = Time.timeScale; // Initialize the previous time scale
Debug.Log("PauseMenu Start - pauseMenu set to inactive");
}
void Update()
{
if(Input.GetKeyDown(KeyCode.Escape))
{
Debug.Log("Escape key pressed");
if (isPaused)
{
ResumeGame();
}
else
{
PauseGame();
}
}
}
public void PauseGame()
{
Debug.Log("PauseGame called");
pauseMenu.SetActive(true);
previousTimeScale = Time.timeScale;
type here
// Store the previous time scale
Time.timeScale = 0f;
isPaused = true;
}
public void ResumeGame()
{
Debug.Log("ResumeGame called");
pauseMenu.SetActive(false);
Time.timeScale = previousTimeScale;
isPaused = false;
}
public void GoToMainMenu()
{
Debug.Log("GoToMainMenu called");
Time.timeScale = 1f; //editing
isPaused = false; //editing
SceneManager.LoadScene(0);
Debug.Log("Main menu loaded");
}
}
I tried using Blackbox AI and ChatGPT to understand each aspect and the underlying issue but no solution hence looking for assistance here.`
Preh Abbasi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.