Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by babasuter · Apr 20, 2016 at 11:26 PM · c#mapdontdestroyonloadpersistence

Zelda Room Switching & Preventing Duplicate Player Spawns from dontdestroyonload

I am making a 2D game that has multiple rooms (scenes), laid out in a 3 x 3 grid, player starts in the center one. Orange dots are exit portals.
alt text
Problem 1: When exiting a room and loading the next room, the player should appear in the new room on the opposite side of where he left.

For example, if player exits right, he should switch scenes to that room, but then be positioned on the LEFT side of that new room, indicating he just exited right (like in the old NES Zelda dungeons)

I tried saving the player vector2 position as a variable, then loading it and inverting either the X or Y, but got lost on how to know when to invert which number.

Problem 2: When going back to the START room, a second player appears. I understand from searching that this is because of my dontdestroyonload on the player, but don't know how to prevent the duplicate. I have the player in the hierarchy with a script that contains:

 void Start()
     {
         DontDestroyOnLoad(this.gameObject);
     }


Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RandomAgent · Apr 20, 2016 at 11:44 PM 1
Share

I'm new to unity but I would put a spawn location on each side of the room and figure out a way to tag them (North, South, East, West.) Then when a scene is about to be loaded , specify which spawn location I want the player to appear based on which side the door was on that he/she exited. (Which would probably also just happen to be a spawn location). Just an idea. If the person exited on the west. The new scene should place him on the east. Etc

avatar image Cherno RandomAgent · Apr 21, 2016 at 08:22 AM 1
Share

A sensible solution, since you can keep GameObjects between scene loading with DontDestroyOnLoad,

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by babasuter · Jun 22, 2020 at 06:31 PM

I found solutions to both of these problems. You can see them implemented by opening room "B2" in this Unity Github Repo

Problem 1: Here's the relevant part of the room switching code:

 private void OnTriggerEnter2D(Collider2D other)
     {
         //FROM A1
         if (other.gameObject.CompareTag("East") && SceneManager.GetActiveScene().name == "A1")
         {
             spawnLoc = new Vector2(-7, 0);
             SceneManager.LoadScene("B1");
             transform.position = spawnLoc;
         }
 
         if (other.gameObject.CompareTag("South") && SceneManager.GetActiveScene().name == "A1")
         {
             spawnLoc = new Vector2(0, 7);
             SceneManager.LoadScene("A2");
             transform.position = spawnLoc;
 
         }

Problem 2: I read that singletons could be used, but I went with this solution, which just checks if there is already a GameManager, then destroys the extra one. There are 2 scripts, GameManager, and PlayerControllerNESW.

In the GameManager Script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class GameManager : MonoBehaviour
 {
     public static GameManager Instance;
     public GameObject player;
 
     void Awake()
     {
         this.InstantiateController();
     }
 
     private void InstantiateController()
     {
         if (Instance == null)
         {
             Instance = this;
             DontDestroyOnLoad(this);
         }
         else if (this != Instance)
         {
             Debug.Log("Destroying extra GM");
             Destroy(this.gameObject);
         }
     }
 
     void Start()
     {
         Debug.Log("Started");
         Debug.Log("Player has not spawned. I'll make one for you.");
         Instantiate(player, new Vector2(-4, 4), Quaternion.identity);
     }
 }
 

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

Answer by Soraphis · Apr 21, 2016 at 08:38 AM

As @Cherno said: you can use "DonDestroyOnLoad". e.g. for you player or whatever.

Do each room in its own scene. Create a "SwitchScene"-Script which knows the target scene, and target position.

when the player enters a trigger-zone:

  • load the new scene additive.

  • teleport the player to its new position.
    • do some fancy stuff like camera swipe or whatever

  • unload the old scene.

in the editor (use the new multy-scene-edit feature) load both scene and position them correct to each other


maybe you need two different scripts for this job. e.g. when switching levels from indoor to outdoor locations.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image aballif · Apr 21, 2016 at 05:55 PM 0
Share

Do you have a script that can be used to teleport the player?

Teleportation would be very nice to use ins$$anonymous$$d of having multiple scenes for the same room.

avatar image Cherno aballif · Apr 21, 2016 at 07:27 PM 0
Share

In this case, teleport just means updating the transform.position of the player.

avatar image aballif Cherno · Apr 21, 2016 at 10:24 PM 0
Share

What would be the best way to update the transform.position. The Unity C# just keeps changing. I get frustrated because all of the tutorials are outdated.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

134 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Return to a specific scene & position 0 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

Persisting player when loading levels 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges