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 Flash Voyager · May 20, 2015 at 11:14 AM · scenescene-loadingdontdestroyonload

Losing prefab transform values on scene open

Hey guys, i'm instantiating a clone of a first person controller in a scene called home, when i start the game, then i go into a scene called city which serves as a portal to other scenes, so far so good i can return to the scene home and the first person controller starts where i want it to, then i go back to the city scene, it also goes where i want it to, i have spawners in each scene that communicate with each other setting the right position for each time the first person controller(player) enters or leaves a scene, so as i was saying when i get out of the home scene into the city scene and then back to the home scene everything goes as supposed to, the problem happens when i go into the city scene and then enter one of the portals present in the city and then go back to the home scene, when i re-enter the home scene the player looses its transform values and is instantiated out of the playable space, and i can't find a solution to setting it to the place it should spawn.

The spawners work as following : the city scene as a master spawner (gameobject-->cube) that runs this script, and other spawners (gameobject-->cubes) which serve as a reference for the spawning position of the player with a simple dont destroy o load script, the master spawner will then move the player to each of the other cubes position

Thanks in advance =)

     public     GameObject emptyObject  ; // Drag empty object here
     
     public bool drop_casa = false;
     
     public bool drop_casino = false;
     
     public bool drop_galeria = false;
     
     public bool drop_mercado = false;
 
     public bool drop_farmacia = false;
 
     
     
     
     public GameObject Player ; 
     
     public Transform Destination_casa; 
     
     public Transform Destination_casino;
     
     public Transform Destination_galeria;
     
     public Transform Destination_mercado;
 
     public Transform Destination_farmacia;
 
     
     
     public bool Esc_casa = false;
     
     public bool Esc_casino = false;
     
     public bool Esc_galeria = false;
     
     public bool Esc_mercado = false;
 
     public bool Esc_farmacia = false;
 
     
     
     
     // Use this for initialization
     void Start () {
         
         GameObject player = Instantiate(Player) as GameObject;
         
         
         DontDestroyOnLoad (this);
         
     }
     
     // Update is called once per frame
     void Update () {
         
         
         if (Esc_casa)    
             
         {        
             
             drop_casa = true;
             Player.transform.position = Destination_casa.position;
             Debug.Log("blabla");
         }
         
         if (Esc_casino)    
             
         {        
             
             drop_casino = true;
             Player.transform.position = Destination_casino.position;
             Debug.Log("bleble");
         }
         
         if (Esc_galeria)    
             
         {        
             
             drop_galeria = true;
             Player.transform.position = Destination_galeria.position;
             Debug.Log("blibli");
         }
         
         if (Esc_mercado)    
             
         {        
             
             drop_mercado = true;
             Player.transform.position = Destination_mercado.position;
             Debug.Log("bloblo");
         }
 
         if (Esc_farmacia)    
             
         {        
             
             drop_farmacia = true;
             Player.transform.position = Destination_farmacia.position;
             Debug.Log("bloblo");
         }
         
     
         
         DontDestroyOnLoad (this);
     }
     
     
     
     
     
     
     
     
     
     
     void OnLevelWasLoaded(int level) {
         if (level == 1)
         { 
             Esc_casa = true;
             
             
             
         }
         if (level == 2)
         { 
             Esc_casino = true;
             
             
             
         }
         if (level == 3)
         { 
             Esc_galeria = true;
             
             
             
         }
         
         if (level == 4)
         { 
             Esc_mercado = true;
             
             
             
         }
 
         if (level == 6)
         { 
             Esc_farmacia = true;
             
             
             
         }
         
     }
 }

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by abhi_360 · May 20, 2015 at 11:33 AM

firstly remove DontDestroyOnLoad (this) from Update

then in your void OnLevelWasLoaded(int level) { if (level == 1) { Esc_casa = true; Esc_casino = false; Esc_galeria = false; Esc_mercado = false; Esc_farmacia = false; }

whats happening is your bools are not resetting so if u go by ur script then the more than 1 boolean is true so in ur update loop if the first 3 booleans are true u will be in galeria's spawnPoint and remember in more than 1 levels the spawnPoint may be the same World Space Coordinate like (0,0,0) and in the other levels it might be different which becomes more confusing.

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 Flash Voyager · May 20, 2015 at 04:00 PM

So i comented the don't destroy on load (this), and its working i can enter the home scene and then go into the city scene and when i go back to the home scene it spawns the player where it should, but now i get another problem for which i tried the same solution but it doesn't seem to solve it, when i'm in the home scene i have another script similar to the one above and it works fine, but when i go back to the city scene and enter another scene say for example the supermarket scene and then i go back into the city it doesn't load the player where it's supposed to (in front of the supermarket entrance), i find its because of the commented don't destroy on load, with it commented it doesn't check what level was loaded and therefore the boolean doesn't plug on/off

Comment
Add comment · Show 1 · 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 abhi_360 · May 22, 2015 at 06:10 AM 0
Share

you dont need the DontDestroyOnLoad(this) on update as calling it on start persists the object throughout all scenes watch this

https://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/persistence-data-saving-loading

view the video for reference. Your problem is your not resetting all your variables to their default values on changing scenes you need recheck all your scripts that have a DontDestroyOnLoad(this) and Add a Reset function which resets all your variables to default values. If you require some kind of example i'll create a sample Unity project on Git if i have time :)

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

DontDestroyOnLoad on disabled object doesn't work? 1 Answer

How to disable Dont destroy on load 0 Answers

How to use one object selected from previous scene to the current scene ? 1 Answer

Cannot close/unload a scene that is open in editor during playmode (using C# code)? 3 Answers

Add Menu to Unity Roguelike Example 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