Reloading active scene with SceneManager on colission/trigger not working.

Updated on June 12, 2017 in [A] Unity Scripting
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
8 on June 10, 2017

As a first little project (besides some messing about with C# console applications), I want to do a platformer. I have run into a bit of a, hickup, though.

I have made a box that goes under the level that acts as a trigger when the character falls down, this is supposed to reload the current scene – This does not happen.

The scene is added in the Build Settings.

using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerDeathReset : MonoBehaviour {
void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.CompareTag("Player"))
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
Debug.Log("Collision detected");
Debug.Log(SceneManager.GetActiveScene().name);
Debug.Log(SceneManager.GetActiveScene().buildIndex);
}
}

The reset script is as above, I added the Debug lines to make sure the collision is detected. As seen below, it is.

(Image not loading? But it shows the console output with the Debug info. The message, scenename being Level01 and the index number of 0)

Both my message, the correct scene name and the correct index number is returned, but the SceneManager line to reload the scene does nothing.

I have been messing about with it for some time now, looking at different forum posts and the documentation, but I can not for the life of me get this to work.

I have also tried to get/set the buildIndex in a variable/integer and use that in the Scenemanager.LoadScene(); – No dice.

I hope what I wrote makes just a bit of sence, but as I am new to this whole programming thing I bet my terminology is somewhat off. 🙂

Thanks in advance.

  • Liked by
Reply
2 on June 11, 2017

Check if both of the player and the collider you will be interacting with have component “Rigidbody2D”.

Btw instead of using SceneManager.GetActive.. thingy you should use Application.loadedLevel

 

Guru
on June 11, 2017

Objects don’t need rigidbodies to collide.

Devoted
on June 11, 2017

Application.loadedLevel is deprecated, don’t use it.

 

Show more replies
  • Liked by
Reply
Cancel
4 on June 11, 2017

Thank you for the answers. 🙂

The issue is not the colission, as the debug data shows, that works as intended. The issue is why the scenemanager does not reload the scene when the colission is detected. 🙂

I could use Application.loadedLevel, but as that will be taken out of Unity because they are focusing on scenes as opposed to “levels” – I don’t see it as a viable solution, as the scenemanager should be able to load the active scene again, i’m sure I am just missing something. 🙂

Devoted
on June 11, 2017

Just for testing, try not to get the current active scene, but type in the scene name.

 

on June 12, 2017

Tried that as well, both scene name and the id it has in the Build Settings. Maybe i should’ve mentioned that. 🙂

Devoted
on June 12, 2017

No, not talking about the build settings. The scene should of course be in the scene settings. But try to reload the scene by typing the scene name instead of finding the current active scene.

SceneManager.LoadScene ("SceneName");

 

on June 12, 2017

Yes, tried it, nothing happens,

I also just tried making a reset button, mapped to the R key, scenemanager still won’t load the active scene.
Something in the script must be wrong, but as far as I can see i have followed the unity docs, really a headscratcher. 🙂

Show more replies
  • Liked by
Reply
Cancel