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 johndis34 · Mar 11, 2012 at 03:26 AM · dontdestroyonloadload levelonlevelwasloaded

change player location when coming back to a level

I am making a project where I need to exit one part of a level such as a door way and when I come back I start from that door way. I've read up on Scene connecting and tried to make that work as best as possible but have problems. One my scene manager game object that holds info I put a Dontdestroyonload on it to keep information but I end up with repeats of of that game object I was pointed towards the direction of Singletons but have no clue what to do with them. I am using a OnLevelwasLoaded to keep track of what end of the level I'm at how do I program that to tell the player what part of the level to start from again

function Awake () { DontDestroyOnLoad (transform.gameObject); } function OnLevelWasLoaded (level : int) { if (level == 12) { print ("backfromdiary");

 }
 if (level == 11) 
 {
     print ("backfromphone");
  
 }
 if (level == 13) 
 {
     print ("backfromtable");
 
 
     
 }

}

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 kalvinlyle · Mar 11, 2012 at 05:14 AM

If you have a do not destroy on load object you need to make sure you don't load more of them with your new levels. Create an empty scene with just your do not destroy objects, in the Awake() function put a call to load your level. Make sure your levels do not have any do not destroy on load objects in them.

On the player Start() function do the check to see what level was the previous loaded level (you'll have to store that in your donotdestroy object and once you set the player position then set that variable to the current level)

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 Kleptomaniac · Mar 11, 2012 at 10:09 AM

What you can do is before you load each level with Application.LoadLevel, log the player's last position to an array of Vector3's, with each element corresponding to the appropriate level. Then, when calling OnLevelWasLoaded, look for the appropriate array element and change the player's position to that position. So for example:

 var player : GameObject;
 private var lastPos : Vector3[]; //In order for this to work, you should call each level as consecutive numbers from 0 - e.g 0, 1, 2, 3
 private var levelToLoad : int;
 private var canLoadLevel : boolean = false;
 
 function Awake () {
     DontDestroyOnLoad(transform.gameObject);
     lastPos = new Vector3[Application.levelCount - 1];
 }
 
 function Update () {
     if (canLoadLevel) {
         lastPos[Application.loadedLevel] = player.transform.position;
         Application.loadLevel(levelToLoad);
         canLoadLevel = false;
     }
 }
 
 function OnLevelWasLoaded(level : int) {
     player.transform.position = lastPos[level];
 }

Created on air, so I don't know if it will work per se. In order for it to work, you'll need to make it so that your levels are named in a consecutive order from 0 (e.g. 0, 1, 2, 3, etc.) and you whenever you need to load a new level in another script, change canLoadLevel to true, levelToLoad to whatever number level you want to load, and then change canLoadLevel back to false. There is probably a better way of doing this, but right now that's all I could think of to keep it nice and short.

Hope that helps! Klep

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 johndis34 · Mar 11, 2012 at 11:30 AM 0
Share

It seems way too complicated. I'm hoping that there is a way to just tell a scene that when loading from level a to level b that it sets the player controller at a specific point. when loading from level c back to level b it sets the player controller to a different specific point.

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

6 People are following this question.

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

Related Questions

OnLevelWasLoaded still called from destroyed gameobject 2 Answers

Bizarre Co-routine errors after re-loading scenes 2 Answers

Is OnLevelWasLoaded supposed to be called twice when using DontDestroyOnLoad? 3 Answers

DontDestroyOnLoad - is it intended behavior? 3 Answers

...Why does this FAIL? - DontDestroyOnLoad()?? 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