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 D4rks1027 · Apr 30, 2012 at 06:33 AM · playerprefsscene-loading

Help with loading player position after scene change

Hey everyone, ive been having trouble loading player positions between scenes. What i have now is an overworld with buildings you can enter, each being a seperate sene, with a trigger that returns you to the over world. What I would like to happen is that when the overworld scene loads, the player is place outside of the building they just exited, however the player keeps being placed at the starting point for the overworld scene. Heres what i have now.

this script is placed on the trigger that sends them to the building interior

sing UnityEngine; using System.Collections;

public class NewLevelChange : MonoBehaviour { public GameObject player; //public LevelManager levelManager;

 void Start () {
     player = GameObject.Find("Player");
     
 }
 
 void Update () {

 }
 
 void OnTriggerEnter(Collider col){
     
     if(col.gameObject.tag == "Player"){
         
         PlayerPrefs.SetFloat("PlayerX",player.transform.position.x);
         PlayerPrefs.SetFloat("PlayerY", player.transform.position.y);
         PlayerPrefs.SetFloat("PlayerZ", player.transform.position.z);
         
         Application.LoadLevel("Level2");
         
     }
 }
     

}

and this is the script that should return them to the correct place in the overworld.

public var player : GameObject; //var newPosition = Vector3(0,0,0);

function Start () { player = GameObject.Find("Player"); }

function Update () {

}

function OnTriggerEnter(col : Collider){ Application.LoadLevel("City"); var newX :float = PlayerPrefs.GetFloat("PlayerX"); var newY :float = PlayerPrefs.GetFloat("PlayerY"); var newZ :float = PlayerPrefs.GetFloat("PlayerZ");

     Application.LoadLevel("City");
     
     player.transform.position = Vector3(newX,newY,newZ);

}

I'm rather new to Unity scripting so it's probably some simple mistake, but any help or advice would be greatly appreciated :)

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
4

Answer by Seth-Bergman · Apr 30, 2012 at 07:21 AM

as soon as you reach Application.LoadLevel, the next level will load, so the script will not continue on to the next line. You need a script in the city level with a Start function that sets the position:

 function Start()
 {
 var newX :float = PlayerPrefs.GetFloat("PlayerX");
 var newY :float = PlayerPrefs.GetFloat("PlayerY"); 
 var newZ :float = PlayerPrefs.GetFloat("PlayerZ");
 player.transform.position = Vector3(newX,newY,newZ);
 }

also, you don't really need PlayerPrefs for this, that's more for between game sessions. simply create a static var in your script for this:

static var playerPosition : Vector3;

then you can access it by the name of the script from anywhere:

ScriptName.playerPosition = player.transform.position; (or vice versa)

if you like, you can still set the player prefs every time, of course.

hope this helps

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 D4rks1027 · Apr 30, 2012 at 05:15 PM

That actually did work, however when i start the game the player position becomes the default of the static Vector 3 (0,0,0). Is there anyway to set the vector 3 to a specific position when the scrip starts, because I'm storing the value of the Vector 3 in a LevelManager that is created when the game starts and has Don'tDestroyOnLoad attached to it

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 Evil-Dog · Apr 30, 2012 at 05:18 PM 0
Share

Then use that position ins$$anonymous$$d of the static one and set its right value in an appropriate Start function somewhere.

avatar image
0

Answer by Seth-Bergman · Apr 30, 2012 at 05:59 PM

as long as you have the data stored properly, you can retrieve it. If the var is static, it shouldn't be reset on load anyway, so I'm guessing you never got the pos stored in it..

in the first script:

 void OnTriggerEnter(Collider col){
 
     if(col.gameObject.tag == "Player"){
 
        PlayerPrefs.SetFloat("PlayerX",player.transform.position.x);
        PlayerPrefs.SetFloat("PlayerY", player.transform.position.y);
        PlayerPrefs.SetFloat("PlayerZ", player.transform.position.z);
        playerPosition = player.transform.position;
 
        Application.LoadLevel("Level2");
 
     }
 } 

if you prefer to store it elsewhere, just replace playerPosition with wherever it's at. Keep in mind having a var static will keep it intact between scene changes, just like DontDestroyOnLoad. DontDestroyOnLoad is useful for more complex entities (like the player itself).

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Restarting game with last score from playerprefs. PLEASE HELP!! 1 Answer

PlayerPrefs and Unlocking Scenes 1 Answer

Move a gameobject after loading new scene? 2 Answers

How to create a main menu with singleton pattern? 2 Answers

The player goes through a door and change scene. If I come back, how can I have the player appear where it left off? 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