Load Scene is not restarting the scene properly - c#

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.

Related

Unity 2D - A* Pathfinding issue, Enemy not following player

I just purchased the A* Pathfinding asset from the Unity store. I have a few questions on getting it set up. My current problem is the AI Destination Setter will only follow the player's "0th" position. It goes to wherever my player starts at and does not update to the current position. (See Image below)
I have a game object prefab called "Essentials Loader" that I drag into every scene. This has things like my Player prefab, Game Manager, UI Overlay, Audio Manager, and other things that will be needed in every scene. I set it up this way for two reasons. 1. It prevents me from forgetting to drag in anything important into a new scene. 2. If I completely re-do the player prefab I only need to swap it out in the Essentials Loader and not in every single scene.
The code in this Essentials Loader checks for accidental duplicates and deletes them to prevent unwanted issues between scenes.
Temporary Solution: I have taken the player prefab out of the Essentials Loader and dragged it into the scene. Now when I assign the "AI Destination Setter" Target transform to the player from the scene and not the project window, it works just fine and follows the player correctly.
My question is: is there a way that I won't have to do it this way? I do plan on completely changing the player prefab a little while down the road and I would hate to have to make that change in every single scene. I would like to still have the player prefab in the Essentials Loader and have the enemy still follow me.

Unity not rendering animation in playmode

My door animation plays all fine in Editor, but in playmode, it seems not to render, but the collisions seem to work fine. Here's what happens.
The Animator is set up like this
The Animation the game should show consists of just moving two children objects, for more info. What did I do wrong, and how do I fix it?
It pretty much seems like your objects are marked as static and therefore the meshes get combined into a single static scene mesh.
Many systems in Unity can precompute information about static GameObjects in the Editor. Because the GameObjects do not move, the results of these calculations are still valid at runtime. This means that Unity can save on runtime calculations, and potentially improve performance.
As the name static already suggests: These objects are static and can not be moved by the Animator.

Animations don't work when I drag them to Unity

This is the first time I try to use animations in Unity so my friend built a campfire in blender and also made animations so the fire seems like it's moving. When he converted it to fbx and sent it to me, I downloaded the fbx, dragged it to the Unity project. I thought that the animations should work and I should see the fire moving when I play the game, but unfortunately the fire seemed like it's a not moving object. I screen recorded it so you will understand the problem better. If anyone knows how to fix this problem, it will really help me. Thank you!
First of all You have to check FBX import settings to be sure, that animation is exist. Select your fbx in Unity and switch to 'Animation' Tab in Inspector. If Animation is exist, You'll see animation import settings. Be sure toggle 'Import Animation is active':
If Your FBX does not have animation, in animation tab You'll see message 'No animation data available in this model'.
If FBX is okay, the next step is checking your fire gameobject. It must contains Animator component with some animation controller. This animation controller will play anim clip from your FBX.
But first af all I recommend your check this: Unity Animation Manual

How to communicate between 2 scenes in Unity?

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.

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.

Categories

Resources