Skip to content

Instantly share code, notes, and snippets.

@samsheffield
Last active September 15, 2021 16:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samsheffield/c9e9cdb59879040d147f71194f6afa6f to your computer and use it in GitHub Desktop.
Save samsheffield/c9e9cdb59879040d147f71194f6afa6f to your computer and use it in GitHub Desktop.
2D Game Design F21 Bonus Materials
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);
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