Triggering Next Scene

Using Build Index

Using the Unity Engine Scene Management, we can trigger the next scene just like with the buttons on the click event. The only difference is, this time we are going to be using On trigger Enter method with an empty game object.

How to setup the trigger

First, create an empty game object, then add an “Rigidbody” and Box Collider component. Remember to check “is Trigger” on the Box Collider, and set the gravity to zero on the “Rigidbody”.

setting up the collider for the perfect spot


Now before we start scripting, it is best to understand the build index and how it is setup in Unity. The build index is basically the hierarchy of scenes within the overall game. To access the build index, go to File > Build Settings.

As you can see, Main Menu is identified as zero and so on. What I want is to have the 2D shooter scene move to the Moon base scene. Therefore index 2 to index 3.

So unlike buttons which can directly jump to a set index number, it isn’t very practical to have several different scripts for each individual level. Therefore in Unity we have a method called “Build Index”. With Build index, I am able to basically add a number ( in this case 1) and the Scene manager would automatically add +1 to the current active index.

So now if I was on a scene with an index number of 2, once I trigger the script it would add 1 to the current, which is 3. Therefore I will enter build index 3. This can be applied forever, so if I had 50 different scenes, as long as they are in the build index in an hierarchical manor, this script would always trigger the next scene.

Now all you have to do is add an On Trigger Enter 2D method, add the “If statement” to check for player name tag. And if the player tag is true, initiate the loading next scene!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store