This post has been wikified, any user with enough reputation can edit it.
Question by
TheSlich · May 28, 2021 at 10:01 AM ·
instantiateprefabscenemanager
Persistent GameManager and object Instantiation
What I'm trying to do is to make the GameManager object persistent between scenes, reading a bit I realized that the DontDestroyOnLoad() method allows this behavior, however I don't understand why it doesn't allow me to instantiate objects in new scenes. The following code perfectly replicates the main problem:
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour
{
public GameObject objectPrefab;
private void Awake()
{
DontDestroyOnLoad(gameObject);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
SceneManager.LoadScene("Scene2");
GameObject instance = Instantiate(objectPrefab);
}
}
}
Comment
Your answer
Follow this Question
Related Questions
Prefab or Scene Base Game Development 1 Answer
Wont instantiate game object 1 Answer
What is Casting in Unity ? 1 Answer
How to find the (original) Prefab of a GameObject? 2 Answers