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.
Related
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.
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.
I have a game project. You have a ball that you can control with arrow keys. Purpose is, you will collecting an object. Object will destroy when i going over it, and respawn in random location of ground. Btw, you need to do that in 30 seconds. I couldnt destroy and respawn part. May you help me? (C#)
You can use collisions to destroy object. Run over it and yield. After that you can create couple of spawn points (for more randomness you can move the spawn points).
I'm not sure if it is possible to create a Game Object in the scene (so it appears in the hierarchy) but I would like Unity to not instantiate (or rather remove it) such Game Object when I press play.
The reason why I want to do that. I'm creating Game Objects as 'enemy spawners' in my game. But I don't want to have those transforms or Game Objects actually in memory while the game is running. So I've created an editor script that search for all the spawner entities, retrieve the information that I need and creates a binary file with it that later the enemy manager reads in run time during the initialization process.
If this is not possible, do you guys have any recommendations on how I could implement something similar?
Many thanks in advance.
You are looking for scriptable objects.
Here are some URL's
ScriptableObject Tutorial from Unity
ScriptableObject documentation
ScriptableObjects can be accesed at any time and don't have to be instantiated before you can use them. So they won't have a transform.
I have created a Game class which handles all transactions of a game (whose turn it is, what items each person has, etc). I would like to create one of these objects and keep it synced between both clients of a Photon Unity network.
First I tried Instantiating a prefab over the Photon network with InstantiateSceneObject() and then changing the attached script. The prefab is then found on both clients, but the attached game script is null on one of the players.
After that I tried adding the Game class to a Hashtable and then calling
PhotonNetwork.room.SetCustomProperties(new ExitGames.Client.Photon.Hashtable() { { "game", g } });
But when I attach my debugger to Unity, I notice the script hangs at this line. It doesn't move on to the next line of code.
I am really hoping I don't have to send every object of my game class as an RPC. Seems really tedious. Any help is greatly appreciated.