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 /
  • Help Room /
avatar image
0
Question by Zeronev · Sep 19, 2015 at 05:04 PM · c#positionscene-loadingtransform.position

Change player position based on loaded scene

Hello, Im trying to make a script to change a player starting position based on a prevously loaded scene, for example.

If I go from the scene 7 to scene 2, I need the player to spawn in the p_PosiblePositions[7] in the sector 2. and if I'm going from the scene 4 to scene 2, the p_PosiblePosition to use should be [4].

I tried using this script I made but, then I realized that the OnLevelWasLoaded only tells the current scene that was loaded, not the previous one.

 public GameObject[] p_PosiblePositions;
 
     GameObject player;
 
     void Awake()
     {
         player = GameObject.FindGameObjectWithTag("Player");
     }
 
     void OnLevelWasLoaded(int level)
     {
         switch (level)
         {
             case 0:
                 player.transform.position = p_PosiblePositions[0].transform.position;
                 break;
             case 1:
                 player.transform.position = p_PosiblePositions[1].transform.position;
                 break;
             case 2:
                 player.transform.position = p_PosiblePositions[2].transform.position;
                 break;
             case 3:
                 player.transform.position = p_PosiblePositions[3].transform.position;
                 break;
             case 4:
                 player.transform.position = p_PosiblePositions[4].transform.position;
                 break;
             case 5:
                 player.transform.position = p_PosiblePositions[5].transform.position;
                 break;
             case 6:
                 player.transform.position = p_PosiblePositions[6].transform.position;
                 break;
             case 7:
                 player.transform.position = p_PosiblePositions[7].transform.position;
                 break;
         }
         
     }

*Each scene has his own individual p_PosiblePositions. Thanks, in advance! If I didn't explained well, please leave a comment!

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

1 Reply

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

Answer by hexagonius · Sep 19, 2015 at 08:52 PM

Two choices here, depending on relevance only during current game or even between game runs:

  • game only: create a static int variable in your choice class and set it to the current level index after evaluation before. statics are class variables and will seize to exist only when the game exits.

  • several games: same thing but instead of a static variable use PlayerPrefs (Unity docs)

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 Lo0NuhtiK · Sep 19, 2015 at 08:52 PM 1
Share

Store the previous level.

ex 1: (using DontDestroyOnLoad())

 int prevLevel = 0;

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

 void OnLevel..(int level)
 {
     player.transform.position = p_PosiblePositions[prevLevel].transform.position;
 
     prevLevel = level;
 }

ex 2 : (using a static variable)

 static int prevLevel = 0;
 
 void OnLevel..(int level)
 {
     player.transform.position = p_PosiblePositions[prevLevel].transform.position;
     
     prevLevel = level;
 }

Edit : you ninja'd me by 3 seconds lol so I just converted my post to a comment.

avatar image Zeronev Lo0NuhtiK · Sep 20, 2015 at 01:17 AM 0
Share

I guess im going to try this :) but, I guess I'll need to have a lot of objects with this script attached and set the "prevLevel" correctly?

But setting the "prevLevel = level" will set the prevLevel int to the OnLevelWasLoaded, thus, the prevLevel will always be the same as the OnLevelWasLoaded int. So I guess this is the same as the script?

Please correct me if I'm wrong. or how the script that Lo0n wrote, works exactly? thanks, in advance :D

avatar image hexagonius Zeronev · Sep 20, 2015 at 09:03 AM 1
Share

You're right, but I thought your code is the only place where that's relevant, so it doesn't matter if it's already the current level.

set it where you call LoadLevel to the loadedLevel then.

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

28 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

Related Questions

How can I change position of instantiate objects (clones)? 1 Answer

Wrong position (always 0,0,0) when Instantiate prefab. 1 Answer

I am trying to say if the Y of my player reaches below 0 change the players position to 0,1,0 it wont work thought and I tried using Vector3 but it says I cant use Vector3 like a method Please help! 1 Answer

Pre-loading scene and display it only when I want 0 Answers

[SOLVED] How to load next level? 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