How to communicate between 2 scenes in Unity? - c#

I would like to make a color system for my player. In my game scene the player can pick up the coins and the amount of coins will be save with playerprefs , but I don' know how can I use the amount of coins in my menu scene.
And I need some help to a player color selecter too. When the player select a color than in my game scene must instiate the player with the color.
Soo I think , i need to know how to communicate between 2 scenes.
Can somebody help me with some tutorial?

There are multiple ways to achieve this, which I feel comes down to a matter of taste: save data to file, use DoNotDestroyOnLoad...
But from what I understand, the recommended way now is to create a "manager scene" which will stay alive throughout the lifetime of your app and pass data to and from your other scenes as they are opened and closed, instead of using DontDestroyOnLoad:
It is recommended to avoid using DontDestroyOnLoad to persist manager GameObjects that you want to survive across scene loads. Instead, create a manager scene that has all your managers and use SceneManager.LoadScene(, LoadSceneMode.Additive) and SceneManager.UnloadScene to manage your game progress. src
See the Unity guide here. Basically you would have 2 scenes open at the same time, at any given moment: the manager scene and whatever the actual active game scene is. Then you can communicate between scripts in the two open scenes via event delegates. The way it would work is:
Player selects color in scene1
Color is sent from scene1 to manager scene via event delegate
scene1 is unloaded and scene2 is loaded
Color is sent from manager scene to scene2
This is the approach I've been using for a project now which looks like this:
"0-Attract", "1-Sealant", "2-Paint", and "3-Conclusion" are my actual game scenes, and "Manager Scene" contains everything that exists in every other scene (thus no reason to kill and respawn them) as well as all of my "manager" scripts which handle the passing of data between scenes.
Note that multi-scene editing can be confusing at first, as there are new things you need to pay attention to (i.e., which scene is currently "active" and what that means) so be sure to go through the unity guide I posed above. Good luck!

you can Call DontDestroyOnLoad method on any object that you want that will remain wen you are moving the a new scene so it will not be destroyed when scene is closing.

Related

Simulating a second, hidden physics scene into the future on a single frame to predict the trajectories of planets?

I want to simulate the trajectories of planets in a seperate scene to find out where they will be in the future. I drew a quick diagram to demonstrate.
Is there a way to simulate 2 scenes separately, hiding one but showing the other? I tried this which says they don't interact with each other, but when I tried it they still collided.
Yes, there is a way, if you put all "hidden" objects, like planets on a reparate layer in unity
and disable collisions between that and any other layers in the
edit/project setting/physics
This way those objects won't have any effect on the rest of your scene.
To visually hide the objects simply disable the rendering of that layer in your scene camera. And that's it, if hope this was helpful.

Load Scene is not restarting the scene properly

I'm trying to restart my game when the player loses, I play a coroutine which makes the player ignore collisions and falling off the platforms. Then when the player click a button, the same scene is reloaded.
SceneManager.LoadScene("GameScene")
But when the scene loads again, the player is still ignoring collisions and falls, it's like the scene is loaded but not the same way when the game is played for the first time.
¿How can I reload the scene properly without closing the aplication and opening it again?
Thank you.
The problem is that you are using Physics2D.IgnoreLayerCollision for this.
This property is global, it affect all scenes, and not related to the specific scene. What SceneManager.LoadScene resets is only property related to the specific scene or the objects in the scene.
You have 2 options here:
Don't use IgnoreLayerCollision. An alternative may be, for example, to disable all colliders on the player. You can use GetComponentsInChildren for example to find all the colliders.
Reset IgnoreLayerCollision manually.

Destroying all the previously created objects when the Game Start over

In my game I am using Destroy(gameObject) for some gameobjects to pass them on another level. Now the problem is after my player dies there are objects left on the scene when I restart the game after the player dies, I only want the objects that i created when the game started.
How can I accomplish this?
Store the player created gameObjects in a list, then call Destroy() on them after a restart.
First of all if you need to destroy object that you marked with DontDestroyOnLoad you are doing something wrong.
Use it only on objects that you really want to keep across the scenes. If you need to make changes in these objects, just change parameters values and make other objects live only in scenes where they belong.

What is the best way to manage Player object of SteamVR plugin for Unity3D when change of scene

I am developping an application using the new version of SteamVR plugin (2.2) for unity and i need to navigate between several scene during the game.
I have for all my scenes a Player object coming from the SteamVR plugin handle input and event, who is a singelton dontDestroyOnLoad, and i don't know the best way to handle this object during the change of scene.
There is 3 options in my opinion :
For all scene a player object is present, but steamVR don't check if an instance already exist so i add this feature to have only one player after scene initialisation.
The other solution is to destroy the current instance before loading the new scene but with the new system of event made by Valve in this version i don't know if it's a good way to manage the fact to have only one player at the time, can potentiolly create event conflict (didn't saw yet).
The last one but not really confident with it is to let 2 player per scene and one of this is the good instance of the singelton class (way more anoying to get the good instance Player, and can i have conflict between them ?)
If someone have some idea or can give me better understood of my conception issue you are welcome :)

Unity - Saving Level Changes

I am making a small RPG and a problem came up when I enter my house where there is a chest and I opened the chest to get an item, I then leave the house which changes the scene and come back into the house with the chest still able to be opened.
What I am looking for is when certain items (such as chests) are opened they stay opened throughout the game even on scene changes. There is probably more than one way to handle this but the only thing that came to mind was maybe using PlayerPrefs but I wanted to get some feedback if anyone had an easier way of approaching something like this.
I suppose, you want to save state of the scene with chest object. You shouldn't allow unity engine to destroy your chest object when entering next scene.
Use this method
DontDestroyOnLoad (transform.gameObject);
Read more on the Unity3D website.

Categories

Resources