2D Game Design F21 Bonus Materials
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Here is a bonus Unity example for 2D Game Design F21. Let me know what else you need! | |
====================================================================================== | |
HOW TO USE A VARIABLE IN THE INSPECTOR TO SELECT A SCENE YOU WANT TO LOAD | |
Full example: TriggerScene.cs | |
Important: | |
1. The new scene must also be added to your Build Settings | |
2. Don't forget to set the string variable in the Inspector (you don't need to use quotes there either) | |
3. Don't forget to add using UnityEngine.SceneManagement; to the top of your script | |
// Create a public string variable that you can set in the Inspector to specify the name of your new scene: | |
public string newScene; | |
// Use the newScene variable to change to the desired scene like this: | |
SceneManager.LoadScene(newScene); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
// Add SceneManagement to switch scenes | |
using UnityEngine.SceneManagement; | |
public class TriggerScene : MonoBehaviour | |
{ | |
// Create a string variable that you can set in the Inspector to specify the name of your new scene. You don't need quotes there. | |
public string newScene; | |
// OnTriggerEnter2D is called at the moment of intersection | |
private void OnTriggerEnter2D(Collider2D collision) | |
{ | |
// If the other thing has a specific tag. It's a good idea to limit the detection to specific things | |
if (collision.gameObject.CompareTag("Player") == true) | |
{ | |
SceneManager.LoadScene(newScene); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment