how to save data while reloading scene in unity 3D - unity3d

I want to reload scene and update just one variable leaving everything else untouched. I also want to peek data from inspector.
I tried the following method:
SceneManager.LoadScene(scene, LoadSceneMode.Single);
but whole scene loses its data. Is it possible to update just one variable while reloading?
Thank you for any advice.

you have to save all the game states which matters before reloading the scene like enemies positions their health,players health and weapon etc and load them then assign them to your in game variables in the start of the scene.
you can achieve this by using serialization techniques

Related

why am I losing all gameObjects in unity when I have more then one scene

I am trying to create a tower defense game and I made a menu scene and a level01 scene.
Now when I create some objects in the menu scene, and go to the level01 scene, I lose all my objects in my menu scene. I don't know what is going on. I have tried to create a new menu scene and duplicating my level01 scene, but it doesn't work.
You can use DontDestroyOnLoad or you can add multiple scenes and never unload the menu scene.
SceneManager.LoadScene("OtherSceneName", LoadSceneMode.Additive);
The use of a scene, as its name implies, is to prescribe a different scene and arrangement. If you can design the gameplay inside a scene and deactivate them beforehand instead of adding objects. This will make the code more consistent and will prevent future problems.
You can mark objects to keep them between loading scenes by DontDestroyOnLoad(myObject). Here is the documentation with sample code:
Object.DontDestroyOnLoad

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.

How can I access an object's components from a different scene?

I have a Player gameObject that has a script on it which keeps some variables inside it. I created a scene which acts as a main menu and has "Shop" part in it, which has upgrades in it, basically I need to access Player gameObject's script from a different scene so I can modify variables from main menu scene. How can I do that ?
check this out:
https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
once your player is loaded this will keep him in every scene until you choose to release hiim, meaning when you load your menu scene he will be there to access.
now since we know hes there, we can:
(note that player is your script with the variables not your actual player)
ScriptName player = GameObject.FindObjectWithTag("yourtag").GetComponent<ScriptName>();
Be default the components are instances of a class. If there isn't a GameObject with that component in the currently loaded scenes, there is nothing to access.
If I recall you can now load more than one scene at a time.
If there is a GameObject with that script currently loaded you can use:
https://docs.unity3d.com/ScriptReference/Object.FindObjectsOfType.html
or even better
use singleton design pattern, in case you have only 1 player at a time.
I guess you want to save these upgrades for the next run of the game, so you can use PlayerPrefs
https://docs.unity3d.com/ScriptReference/PlayerPrefs.html
If you want to save more than 50 values of data I would suggest using a file based DB like SQLite, it takes little time to setup and works very fast. And it is a lot more readable than using PlayerPrefs.
How about static class?
You can keep values there and access it from any place you want :D

How would I load a Canvas from another scene in Unity?

What I'm trying to do inside this script in Unity is to load a different scene, and find the canvas from that scene and enable it. I already know how to enable the canvas from the same scene but the issue is how can i find and enabled a canvas from another scene.
I tried SceneManager.LoadScene(scenename); to change scene so what should I add or modify? Can someone help me?
Save the canvas you want to switch to as a 'prefab' in your assets folder.
If you are loading a new scene without carrying over data from the last scene then just set up the canvas in that scene using the editor.
If you are carrying data over, or perhaps a player gameobject, then you can instantiate the canvas prefab you need. This would create it in the new scene and you can have a reference to it from wherever you chose to instatiate the prefab.
Alternatively you can have multiple canvases on the same gameobject and switch them on or off as you need them. Very useful for menus.
Hope that helps.
There're many solutions:
Using MonoBehavior's DontDestroyOnLoad(transform.gameObject) for your canvas you need to be on the next scene; You can find a lot examples in Google. Here's the link : https://docs.unity3d.com/ScriptReference/Object.DontDestroyOnLoad.html
Saving somewhere your current scene's gameobjects(canvas) and loading it again and recreate on the next scene as serialization etc;
Same as #2, but save required data in local variables.
There are two ways I understand your question.
You either want to copy a game object with all it's parameters as defined in the editor into another scene.
The best way to do that is obviously to use prefabs. (where to start?)
If you want to copy a game object and all its parameters after they have been modified at runtime, then you could use DontDestroyOnLoad() but that would mean the source scene has to be loaded at least once before the destination scene.

Switch between scenes but keep player position when comes back?

My game will switch between two scenes : scene A and scene B;
scene A is a world where the hero can walk around and trigger battles;
scene B is the battle scene;
when the battle finished, I want to turn back to scene A and hero should be in the position where it trigger battles. So I need to save scene A before I load scene B;
I tried the api LoadSceneMode.Additive; But it's just used to mix one scene to the current loaded scenes.
Could you help me plz?
Firstly DO NOT use "additive". Just use ordinary scene load.
Secondly you have the problem of "remembering" where the guy was when sceneA loads.
Your easiest approach to get you going .. learn about PlayerPrefs.
Just before you quite sceneA, save the hero's position. When you load sceneA, get the hero's position.
Alternately you can use one static class as a sort of global to keep track of the info. But to do that you have to learn about writing that sort of code.
Be aware that what you're doing is not that easy - Unity is a lot harder than it says on the box.
I encourage you to master PlayerPrefs in the first instance, because you will have to use it all the time anyway.