- Home /
How Do i Get Rid Of A Scene Without Deleting It's Objects
so basically in my scenario i have an enemy encounter similar to pokemon but when i load the next scene after the said encounter i dont want it to destroy and reset the previous scene and i am completely unsure of how to do this so can the professionals please show me how to do this one i am fairly new to unity but im willing to learn thanks :3
Answer by Jack-Mariani · May 24, 2019 at 06:51 AM
Prefabs should not be used to save the current state of a gameobject and load it to the next scene.
For example, if you load a prefab of an injured enemy this would be reverted to full state in the next scene.
Also there's no way in the scene to save all the gameobject and bring them in the next scene.
BUT, you can you can add DontDestroyOnLoad to each Game Object you want to save to keep it in the next scene.
From Unity Manual DontDestroyOnLoad.
The load of a new Scene destroys all current Scene objects. Call Object.DontDestroyOnLoad to preserve an Object during level loading. If the target Object is a component or GameObject, Unity will also preserve all of the Transform’s children. Object.DontDestroyOnLoad does not return a value. Change the argument type using the typeof operator.
You can also use Tags to define a group of gameobjects to be saved if you want to save a group of them.
The code would look like this:
public class DontDestroy : MonoBehaviour
{
void Awake()
{
GameObject[] objs = GameObject.FindGameObjectsWithTag("YourTag");
if (objs.Length > 1)
{
Destroy(this.gameObject);
}
DontDestroyOnLoad(this.gameObject);
}
}
Sorry i misworded my question it was a little late but i sortof want to pause the previos world while in the next area fighting the encountered object sort of like an arena that you go to but im not sure if you can disable scenes to make them not visible or do i just have to put the arena under the map and make the player fast travel to there or what sorry if im not getting my point across clearly its kind of hard to explain everything over a simple keyboard :3
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Moving a inspector slider to add objects to the scene 1 Answer
Access Inventory from player 1 Answer
How to display an image above 3D object, 0 Answers