Coroutine not starting when loading from a different scene - c#

I have a character selection screen that displays a confirmation screen and when selecting, a coroutine is started, a burst of particles fly into the screen and the new level is loaded - this is to delay the loading of the level so the particles can fill the screen.
This all works as intended when I test it by directly loading the scene (character selection) on the Unity editor, however, if I'm doing a full test and I come from (Start Screen) I can do everything and the debug says it is registering everything, however the particles don't show up and the loading doesn't execute.
Also, the console doesn't display any errors.
I've tried everything (Stop coroutine, using a while(true) ...) but can't figure out why it doesn't load. Code below:
The UI button has LoadEloise() as the method that gets triggered when clicking on it.
Code below:
Tried stopping coroutine right before activating it and also on Start, also making sure that the particle system is set to Active(false) before then being activated again.
IEnumerator DelayLoad()
{
heartExplosion[explosionNumber].gameObject.SetActive(true);
yield return new WaitForSeconds(1);
SceneManager.LoadScene(sceneName);
}
public void LoadEloise()
{
sceneName = "Eloise";
explosionNumber = 10;
StartCoroutine(DelayLoad());
}
Expected result: particle system is activated, covering the screen, and then after one second new scene is loaded.
This is a video showing the expected result, achieved by starting the test from the scene that hosts the coroutine: https://www.youtube.com/watch?v=BQAgzJ-HJB4&feature=youtu.be
Actual result: nothing, no errors, no particles nothing. The button is shown as pressed from the graphic point of view but nothing happens.
This is a video showing what happens when starting the code from the previous scene: https://www.youtube.com/watch?v=WhEzi9Qjvg8&feature=youtu.be

Is your LoadEloise function even getting called?
If it was just a matter of the particles not getting displayed then it could have been a case of something like the canvas sorting order not being set properly for the particle system/container but if the subsequent scene loading is also not happening then you need to check two things first:
Add breakpoints/debug statements to follow the order of the execution of code and make sure that the correct functions are getting called in the order they are supposed to.
If all the functions are getting called and you don't see any error related to scene loading then it could be related to how the scene is set up when loaded externally. Add breakpoints/debug calls in the initialization function for the new scene to ensure that it has been loaded and is in place, make sure the position/scale of the content is not set to some weird value when the scene is loaded and check the hierarchy to locate the scene content in the editor window to make sure that the visual elements are set up properly.

Related

Steam VR Scene stops responding on reload in Unity

I have a scene that I'm working on using Steam VR 2.0, and Unity 2018.3.2f1. I have a simple statement in it that reloads the scene
private void Update()
{
if (Input.GetKeyDown(KeyCode.R))
{
SceneManager.LoadSceneAsync("Final");
}
}
The issue is: when I reload the scene, it stops responding properly. I am still able to move my head around, and hover over objects. And the objects I hover over get highlighted, but they stay highlighted. I'm not able to pick them up, or interact with them in any meaningful way, and I don't know why this is happening.
I've attached a screenshot of the issue below.
As you can see, multiple objects are highlighted, and the hand mesh is weird:
Solutions I've tried--
Using LoadScene instead of LoadSceneAsync
Using Application.LoadScene instead
Tried to edit the Player script in SteamVR library to not add it to Don't Destroy On Load
Any suggestions?
The issue was arising because the Player prefab in SteamVR 2.0 had Do Not Destroy on Load checked. So, there were multiple players being instanced when I reloaded the scene. I unchecked that box, and now everything is in order.
The checkbox is located inside the [SteamVR] object under the Player prefab:

Load the last played scene where user lost instead of loading the start scene all over again, Unity Block breaker game

I have made block breaker game in unity 4.6 and i have added 3 levels. If anyone loses in any level say level_02 then the lose screen showing game over loads up. When he clicks try again it loads the start screen as i have ordered them in my build settings. I want my game to load the same level where the user have lost so that he clicks try again and the same level loads again i.e level_02.
You can save the current levelScene name before you loads the looseScene like,
PlayerPrefs.SetString("LastLevelName",ScaneManager.GetActiveScene().name);
and when you click the restart button in looseScene then call some method like this,
public void RestartSameLevel(){
SceneManager.LoadScene(PlayerPrefs.GetString("LastLevelName","DefaultSceneName");
//"DefaultSceneName" can be your StartScene name
}
You can restart the scene
Application.LoadLevel(Application.loadedLevel)
Or
Scene scene = SceneManager.GetActiveScene();
SceneManager.LoadScene(scene.name);

In Unity, when I load a scene a second time, some objects don't show up

I have two scenes: Menu and Game. I'm using C#.
When you die in the game, you get sent back to the menu with
SceneManager.LoadScene("Menu");
There's a script with a GUIButton in the menu that, when clicked, loads the game with
SceneManager.LoadScene("Game");
What I want to happen is that when I click the button in the menu scene, it loads the game as if I just clicked the play button with the game scene open in the editor.
What instead happens is that it goes to the game scene, but some objects from the game scene appear to be missing. I'm not using DontDestroyOnLoad() anywhere.
Some objects from the scene do appear, but others don't. The weirdest thing is that there are some data fields on a script on the missing object that are referred to by some other scripts, and those give values that make sense.
Does anyone have any idea what's going on, or what I can do to get the desired result?
Turns out that the problem was that I was destroying one of my objects if it was not the first time that object was created, and I didn't realize it.
are those static objects or dynamic? try to instantiate them in function which gets called on scene loaded.

Screen flashes when loading new scene

I have a level selection menu, inside this I have buttons to load my scene levels.
I am using the next method to switch the scenes:
UnityEngine.SceneManagement.SceneManager.LoadScene();
The problem is that I have dark-gray faders in my scene and when I load the different scenes the screen flashes white. Instead of staying in the same dark-gray tone like I would want it.
I will gladly supply any further information that you need.
Does anyone know a solution?
The last frame of the previous scene is contiguous with the first frame of the next scene so one needs to ensure that there is nothing in either frame that could make a visible difference e.g. resetting the state of something like a material colour via a script called from an Awake function.

Unity; animation on prefab

Tried to find this kind of problem around the net but failed so...
Here's the thing - I have a prefab gameobject that is to represent a unit, portrait more specifically. It has several scripts attached and an Animation component with two animations: Static and Selected.
The prefab is instantiated, moved freely and, after placing, it can be clicked to select it, which should, aside from executing a bit of code, start the Selected animation.
Using this code:
void OnMouseDown(){
//
//Some inside stuff
//
if (this.GetComponent<UnitHandling> ().thisUnit.Selected)
this.animation.Play("Selected");
if(this.animation.IsPlaying ("Selected"))
Debug.Log("Animation of selection is playing");
}
I checked that the animation seems to be playing (at least the Debug message is showing) but I see no animation... What am I doing wrong?
Try making an animation state using mechanim, and play it using this:
GetComponent<Animator>().CrossFade("Selected", 0);
https://docs.unity3d.com/Documentation/ScriptReference/Animator.CrossFade.html
https://docs.unity3d.com/Documentation/Manual/MecanimAnimationSystem.html

Categories

Resources