Do Meshes get reloaded when switching scene? - unity3d

Lets suppose I have a scene with several meshes like trees, boxes, etc. lets call it level 1.
After I finish playing level 1 I switch to level 2 like this.
SceneManager.LoadScene (1, LoadSceneMode.Single); //level2 scene index is 1
The thing is level 2 uses the same meshes level 1 uses. Do they get unloaded -> reloaded?
I'm not talking about DontDestroyOnLoad, as I dont need the same instances of the GameObjects in the level 2 scene.
I'm talking about the Model Data of each object (the meshes), not their instances. I know the instances are deleted, but I think Unity someway knows the next scene uses the same Meshes and keeps them. I just need to know if the Meshes are unloaded and reloaded when switching scene.

Objects that you don't call DontDestroyOnLoad on are destroyed once you change the scene using LoadScene (as if you called Destroy on them all), so yes the get reloaded. They are destroyed then (if they're present in the next scene) they're re-instantiated.
If you do edits to a specific object instantiated in a scene then these edits belong to this object.
If you load the same scene more than once then this specific object will have your edits. However if you load a different scene that has this prefab then the prefab will be a new one without the edits.
If you want all object instantiated to be like this object you have to edit the prefab. To do this you just click the "apply" button.
This will make all objects that become instantiated have the edits.
If you want some object to have the edits and some not to have them you make a separate prefab, just drag the "name" of this object from the hierarchy into your project folder. You've just created a new prefab and you can use it.
Also remember that this doesn't apply to edits done within the game. These are edits that you do (as a developer not as a gamer) in the scene view. Game view edits (for example if a player squishes a cube) are lost after game close.

Related

How to duplicate a live 3D object in another place in a scene?

I have built a game object in Unity that updates its appearance every time the user touches it in VR (collision). What I want is to show the replica of that object in another place in the scene so that whenever the user touches the first object, I see the second object changing in appearance as well. Is there a faster way of doing this without creating a copy of that object in the scene beforehand and changing it every time a change happens in the first object. I suppose I'm asking if Unity has a way for me to show duplicate copies of a game object in a scene without having to create them in the scene beforehand.

Instantiate original Prefab based on an already existing Prefab in game

I am using Prefabs for game levels.
To debug a level I just drag the prefab into Hierarchy view and press [play].
To test level I need an option to replay the prefab, but since I drop it into Hierarchy I need a way to grab the original prefab and Instantiate it again. (and destroy current active prefab).
To find current Active Prefab on Play I just use:
GameObject gameObj= GameObject.FindWithTag("Level");
How do I get the original Prefab and Instantiate it again when pressing 'REPLAY' (button to reload prefab).
I did try to use GetCorrespondingObjectFromOriginalSource
https://docs.unity3d.com/ScriptReference/PrefabUtility.GetCorrespondingObjectFromOriginalSource.html
This will always return null.
Objects in the scene don't have references to the prefabs they come from at runtime. Unity breaks these references when the scene is run (or built). PrefabUtility is an editor-only class for writing editor code.
If you want to do something similar, I'd suggest creating another script that has a reference to the prefab you wish to spawn in. This script can then instantiate that prefab in as a child when the game starts (in Awake or Start) and could have a method on it for destroying the instance and instantiating it again when you want to restart your level.

Can't apply prefab changes

i have a problem when try to apply changes.
I have a GameObject in the hierarchy, i put other GO in a custom script and push apply button. All has saved, but 2 game object can't apply canges.
Thanks in advance.
If you add new objects to the transform hierarchy, those new objects can't reference the prefab because they don't know they're part of the prefab. You can tell this because in the screen hierarchy list the prefab items are blue and the rest are not. You have to save the changes from an object that is part of the prefab first or redefine the prefab by dragging the parent object onto the prefab on the project list. Because a direct save was possible, it will overwrite.
The same is still essentially true for removing objects from the prefab hierarchy, except Unity knows that this is a breaking change (because it wouldn't be mappable to the prefab anymore) and informs you that doing so well remove ALL of the objects from the prefab upward reference and you can only save the changes by redefining the prefab by dragging it from scene to project (and possibly receive another warning about the objects being different: are you sure you want to overwrite?).
Likely those Text and Transform objects are found outside of the prefab. Prefabs can't reference external objects, since there's no guarantee they'll be available for every instantiation of the prefab.

Unity Pattern For Moving Some Objects Between Scenes?

I'm looking for a pattern to transition between scenes, moving one instance of an object to the next scene and then later returning it to the prior scene, but without destroying the other objects that were in the prior scene. Here is the context:
When my game loads, it connects to my server to get the list of characters the player can control and instantiates them dynamically from a prefab.
The loading script moves each character game object to the main scene and then loads that scene.
The main scene now has the characters, which can be interacted with. So far, so good. One of the commands is to pick a specific character and send them on a task, which has its own task scene.
I can move that character to the task scene and move them back when the task is completed. However, the other characters who were not on the task and should not be in the task scene are now destroyed because the main scene was unloaded.
I'm looking for a pattern to accomplish this. Most of what I've read suggests using DontDestroyOnLoad, but this actually moves all characters into all scenes, which results in too many characters in the task scene. Another option might be to create a game object that holds all the character information, pass that between scenes and have logic in each scene to re-instantiate the appropriate character[s] in that scene. This feels like a lot of overhead since I have no other reason to be wiping and recreating them constantly. Perhaps a third option is to restructure my game so the task scene is just added to the main scene and shows up as some kind of overlay that blocks/captures input. This sounds messy and likely to have performance issues (targeting Android).
Does anyone have a good pattern for this?
with the scene manager you can create an empty scene, in this empty scene you put all of the objects you want to keep.
After you unload your first scene (not the one you create) and you load the next scene.
When your next scene is load you transfer all the object from the scene you create to the scene you just load.
PS : you have multiple scene load at the same time check this link for more informations :
https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.html
Turning comments into an answer.
Static variables can be accessed from any scene. Depending on the number of each class type you need, you can either instantiate a static class, or a static List/Array of class. Once you add your objects to it, you can access them from anywhere.
As long as this doesn't become a memory concern (thousands of large objects, eg), this should work well for your purposes (a few characters that need to persist throughout the game anyway).

Object resetting after loading a scene for the second time in Unity [duplicate]

This question already has answers here:
Unity game manager. Script works only one time
(5 answers)
Closed 6 years ago.
I have a game with three scenes, two level scenes and a menu scene. When I press play on my level scene the first time, everything works perfectly, but when I go to the menu scene and then later return to the level scene,
the references on the scripts attached to the NetworkManager GameObject were reset. On the scripts shown in the picture below, for example, all but the references that were linked to prefabs in my assets were reset.
I've tried assigning each and every variable with code instead of doing it manually, but even that doesn't work.
This GameObject does have dontdestroyonload on. Why are these references getting reset specifically on this object?
I don't have enough rep to comment, but did you save your scene after you added the references to the objects?
Answer (By Aaron Ge): I unchecked Don't Destroy On Load and Run In Background, and then my issue was fixed. I am unsure of how this fixed my issue, but it did.
Possible Explanation: http://answers.unity3d.com/questions/734445/scripts-in-other-scenes-still-executing.html
If you use DontDestroyOnLoad on objects that are placed in a scene, make sure that you either:
Load that scene only once. That's usually called a loading scene. The scene just contains certain manager objects and it immediately switches to the menu scene / whatever... You would never load that first scene again.
If you want to load the scene again and you used DontDestroyOnLoad on one or more object, you have to destroy the objects manually. You can't prevent the objects from being created again since they are part of the scene. Usually al objects get destroyed automatically when you load a new scene, DontDestroyOnLoad prevents that.
I unchecked Don't Destroy On Load and Run In Background, and then my issue was fixed. I am unsure of how this fixed my issue, but it did!