(C#) How do I "Do Things" after loading the scene in Unity 2018.2.20???
I've been searching online but methods happen to be outdated or not working in our day Unity version (2018).
I have a character in each scene, so when I enter the trigger on scene A to scene B, I want my character (new character in scene B) to change position. This way, I want to make multiple entrances between scenes without storing the data.
But I have no idea how to since how and where do I assign f.ex. game object that player can use as the destination since previous scene is getting destroyed and stuff... And I have never worked with DontDestroyOnLoad
dont destroy on load is only for persisting gameobjects between scenes. scene 2 will call all the awakes and starts so you could just search for a transform using a tag in the awake event. or why do you need to have the character persist from scene a to b? why cant you find in the scene or maybe use an static claass
Answer by AaronBacon · Feb 26, 2019 at 06:01 PM
Well firstly, you dont need to "Work with" DontDestroyOnLoad(), literally just put
void Awake()
{
DontDestroyOnLoad(this);
}
And the object the script is on will no longer be destroyed on scene change.
Now as for the Multiple entrances, any newly loaded in object will be calling the Start() and Awake() function (any object that wasnt loaded in the previous scene). You can use this to your advantage. The way I would do it is to have a static boolean value on the player or another object that exists across scene change (Using the DontDestoryOnLoad() function) called "loadIn" that is set to false. Then, when you interact with the door in SceneA, it should set the loadIn value to true, then change the scene.
Some object in SceneB should then have the Awake() function checking if loadin == true, (i.e has the player come from a door and needs to be teleported somewhere before loading in). If it finds it is true, it needs to teleport the player.
Now as the player isn't going to be in SceneB, you'll have to use code to find the player gameobject eg:
private GameObject player;
void Awake()
{
//Either:
GameObject.FindGameObjectWithTag("Player");
// Find the Player by Tagging it in the Inspector
//Or:
GameObject.Find("Player1");
// Find by it's name in the scene Object List
}
After you have the players gameObject, you simply need to tp it to the door in question if loadIn is true. There are a few ways to do this, but how I would do it is to create a doorID on each door in a scene. then give the players script a static int called "goToID". then, when the scene loads in, have every door check first if if the player should load in to a door, and then test if the players goToID, is the same as that doors ID in the scene. Eg:
public int DoorID;
public GameObject player;
private void Awake()
{
player = GameObject.FindGameObjectWithTag("Player");
if (PlayerController.loadIn == true) // "PlayerController" is the nameof the players script.
{
if (PlayerController.goToID == DoorID)
{
player.transform = this.transform;
//Teleport the player to this object
}
}
}
After that, simply tag the player with "Player" in the editor, then make sure that the part of the Door script that takes the player to the next scene has a line to set both loadIn and and DoorID on the player. Hope this helps, but feel free to ask if you need anything clarified