-1

my character enters the door which takes them to stage 2 but when the character goes back to the portal which goes back to stage 1 they go back to where the game started and not in front of the door

I tried to make my character move from the second scene to the first, but to the place where she entered the second scene

derHugo
  • 83,094
  • 9
  • 75
  • 115

1 Answers1

1

Welcome to stack overflow, and presumably, programming.

What you are looking for, is a way to 'transfer' data between scenes as they switch. Though there is no direct way to transfer data between scene, your desired effect can be done by

  1. Having a globally accessible Singleton or Static instance.
  2. When a scene loads, a script will read data from the instance (in your case, it will read to determine the player's starting position)

Static Types

In your case, I recommend using static types, you can look into this blog or this short video.

public static class GlobalData {
    // Use an Enum if you need to represent more states,
    // or you can setup a more sophisticated state machine if necessary.
    public static bool PlayerCameFromStage2 { get; set; }
}
// In your player script...
public class YourPlayerScript: MonoBehaviour {
    void Start() {
        // Access the global static data here...
        if (GlobalData.PlayerCameFromStage2) {
            // Move to other starting point...
        }
    }
}

If you would like to look into singleton, I recommend checking out this youtube video, and then looking into DontDestroyOnLoad.

As Singleton relies on having a GameObject "store" your data, which will be destroyed whenever you switch scenes by default.
The DontDestroyOnLoad ensures that the GameObject doesn't get destroyed whenever scenes are switched.


P.S. The programming Jargon you are looking for data persistence, as you would like some data to persist when changing between scenes. Which you can use said data to check where the player needs to spawn at.

WQYeo
  • 3,973
  • 2
  • 17
  • 26
  • Good answer. However, consider a simpler solution first: iin this case it would probably be enough to just manually place the Player object at the location where they should start in the second scene. – KamielDev Aug 05 '23 at 14:41
  • @KamielDev OP wanted to allow the player to backtrack from stage2 into stage1, the difference is requiring the player to be spawned in a different spot. Copy-pasting a new Stage1 and editing the player is a valid solution, but its inflexible and not a good practice for OP to learn *(If OP decides to do edits on stage1, edits have to be done twice, etc)*; In this case, using static is the simplest solution, OP just has to set a static variable, read from it on stage load, and a simple if-else statement to set player's position for stage1. Plus, introduces OP on how to use static instances – WQYeo Aug 05 '23 at 14:58
  • you are totally right, I must have misunderstood the question. – KamielDev Aug 05 '23 at 21:57
  • 1
    There is quite a couple of more solutions ;) See linked duplicate https://stackoverflow.com/questions/32306704/how-to-pass-data-and-references-between-scenes-in-unity – derHugo Aug 07 '23 at 10:43