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 Kag359six · Nov 03, 2011 at 12:44 AM · spawnsceneslevels

remembering variables between scenes

I'm having an issue with creating a spawn point after a level has loaded. So basically, after level 1, it loads level two and loads the default spawn, then after a bonus level, its suppose to return to the 2nd level but with a different spawn. however it keeps using the default spawn. heres my script:

function OnLevelWasLoaded (level : int) {

 if(level == 2)
 {
     loadedPipeLev = true;
 }
 
 if(level == 1 && loadedPipeLev == true)
 {
     Debug.Log("loaded level 2");
     transform.position = level2Spawn.transform.position;
 }

}

any help 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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by syclamoth · Nov 03, 2011 at 01:05 AM

Put the information about what spawns and what doesn't and where in a script on an empty GameObject, and then in your preloader scene (in its start function) use

 DontDestroyOnLoad(gameObject);

to stop it getting despawned every time you load a level.

This way you can carry data between scenes, without having to do something cumbersome like use PlayerPrefs.

Comment
Add comment · Show 3 · 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 Kag359six · Nov 03, 2011 at 09:11 PM 0
Share

the problem is when i return to level 2, there are now 2 objects in the scene ins$$anonymous$$d of the one. which means my boolean counters itself and is set to false, preventing my respawn

avatar image Kag359six · Nov 03, 2011 at 09:48 PM 0
Share

maybe i should explain. when the player gets through level 2, and goes to the bonus level, after the bonus level the player has to go back to level 2 in a different spawn though. I need a script for that. How would i approach that task?

avatar image syclamoth · Nov 03, 2011 at 11:11 PM 0
Share

What I'd do, is have a 'Level Zero' which gets loaded at the very beginning, which contains your 'spawn whatever and where' script, which upon loading tells it to go to your 'Level One' (after using DontDestroyOnLoad(gameObject)). You then never go to that level again, so the object doesn't get loaded a second time!

Or, if you want to use it like a hard reset, have an 'OnLevelWasLoaded' function which tests for

 if(level == 0) Destroy(gameObject);

and then to reset the game, just use Application.LoadLevel(0);

avatar image
0

Answer by CreativeStorm · Nov 03, 2011 at 01:11 AM

You can save the spawn points coords using PlayerPrefs like so:

 PlayerPrefs.SetFloat("SpawnPosX", 10.0);
 PlayerPrefs.SetFloat("SpawnPosX", 10.0);
 PlayerPrefs.SetFloat("SpawnPosZ", 10.0);

or using an gameObjects Vector3 with preventing its destruction:

 function Awake () 
 {
 DontDestroyOnLoad (transform.gameObject);
 }

Or you save using one of this methods an int to indicate an spawn pont in the players awake/start function.

hope that points you an solution ;)

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

How to spawn text when returning to the main menu scene from a level scene? 1 Answer

Reduce 'Levels' or Scene File Size 1 Answer

Scene changing and multiple spawn points 1 Answer

Online levels database 0 Answers

Deleted a scene, cannot create new one 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