Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 altair2020 · Mar 28, 2011 at 04:48 AM · gameobjectsloadlevel

keeping gameObject Positions during scene changes.

Hi,

I am developing a game where the player object enters an area and can goto the 'shop' scene when you exit from here to the game scene i would like to keep the player object at the same position as it entered but application.load seems to load the original scene.

How do you keep gameobjects at the same position during Application.LoadLevel changes ?

Comment
Add comment
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

3 Replies

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

Answer by Jesse Anders · Mar 28, 2011 at 04:59 AM

There are various ways to cause data to persist between scenes. Three of the most common are:

  • Use static variables
  • Use an object marked as 'don't destroy on load' (using the DontDestroyOnLoad() function)
  • Use PlayerPrefs (data stored using PlayerPrefs will persist between runs of the application as well)
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
0

Answer by Adityaunity3d · May 29, 2017 at 10:29 AM

I Use this Script

 public class Teleport : MonoBehaviour {
     public string Scene;
     public GameObject Player;
 
     public float xPosition;
     public float yPosition;
     public float zPosition;
 
     // Use this for initialization
     void OnTriggerEnter () {
         Application.LoadLevel(Scene);
     }
 
     // Ketika Kembali ke Scene Sebelumnya
     void OnLevelWasLoaded () {
         Player.transform.position = new Vector3(xPosition, yPosition, zPosition);
         DontDestroyOnLoad(this);
     }
 }


But I have Problem , I use DontDestroyOnLoad(this); and Every Object was spawn on the new scene ,

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
0

Answer by astracat111 · Feb 07, 2020 at 04:06 AM

I have one Manager game object and script that manages a database. The database class/object stores positions, rotations of game objects. I usually just have characters and important game objects positions and rotations stored because

  [System.Serializable]
 public class Database { 
     public List<Scene> Scenes = new List<Scene>();

     [System.Serializable]
     public class Scene { 
          public List<Character> Characters = new List<Character>();

          [System.Serializable]
          public class Character { 
                public string Name;
                public Vector3 position;
                public Vector3 eulerAngles;
          }
     }
 }


Then just do:

 public Database gameDB = new Database();


Then you make public methods inside your Database class like 'AddCharacter', 'CreateCharacter', and then you call them doing gameDB.AddCharacter("characterNameGoesHere");.
I usually create a Database class and a SM class (for 'SceneManager' but in Unity that name is taken).
You have your Scene Manager load scenes with like SceneManager.Scene_Goto(string sceneName). You have a state machine for your Scene Manager that runs through, grabs all the game objects that are characters in your scene and stores their positions and rotations on loading the scene before you reveal the scene. If you build this kind of custom scene manager you can always re-use it for multiple games as well.
Keep in mind you can't serialize the GameObject class so you need to create your own class like this called either Character or Entity or whatever. You need to put [System.Serializable] at the beginning of each class that you want saved to a file.
You then create in your custom scene manager a save game method like sm.SaveGame("GameName"); for example. This is where you will use a method that stores the positions and rotations of all of your game objects that are characters or entities in your scene.
What I do is name my game objects in the level editor "O_" then a game object name. I have only the game objects with this prefix have their rotations and positions stored in the database. When you save a game or load a scene you just scan through all the game objects in the scene and look for the "O_" prefix using substring like if (gameObject.name.Substring(0,2) == "O_").
This way you won't have this insanely large database, only the game objects with a prefix are stored to the database.
Now you can have an optional argument for your custom scene manager's Scene_Goto() method or whatever like Scene_Goto(string sceneName, bool storeCharacterData). This now gives you the option of either storing the character data (positions and eulerAngles in this case) or not using a bool.
This is for me the most ideal way to save, just creating a custom Database class and a custom Scene Manager class for managing scenes (I still use the Unity default Scene Manager for loading, making certain scenes persistent etc... by the way).

The idea is that you want to whittle it all down to these very short simple and organized methods like this:

 currentScene.GetCharacter("George").setName("Greg");

That's the whole idea with C# object oriented programming, it makes it insanely neat, simple and organized so you have this robust easy to use means of quickly writing code in the end.

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

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

2 People are following this question.

avatar image avatar image

Related Questions

Best way to save the postition of 800+ GameObjects? 1 Answer

Two gameObjects in same place [Problem] 1 Answer

Using layers to ignore GameObjects? 1 Answer

Why can't I apply the textures to the already made objects in BIG Environment Pack Vol. 2 0 Answers

how can i show underwater gravity on my gameobject GetMouseButtonDown(0)? 0 Answers


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