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 fletch254 · Dec 05, 2014 at 06:39 PM · scene-switchingscenes

DontDestroyOnLoad( ) Issue with Instantiation

I currently have two scenes. I have my player prefab in one, that holds the score, weapons collected etc and I want to make it persistent through all the scenes. I tried using DontDestroyOnLoad() but now when I switch from the first scene to the second, the player object gets spawned underneath my map and then falls indefinitely.

I want to know if there's a way to make sure that whenever I go between my two scenes that the character is re-instantiated in the same place (right in front of the door), and also doesn't end up with multiple copies of my player object when reloading those scenes multiple times.

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
Best Answer

Answer by jenci1990 · Dec 05, 2014 at 07:33 PM

Add this function (or void) to your player script:

 function OnLevelWasLoaded (level : int) {
     // You can set the start position in different level
     if (level == 0) transform.position = new Vector3(0f,0f,0f); 
     if (level == 1) transform.position = new Vector3(0f,0f,0f);
     //...
 }
Comment
Add comment · Show 2 · 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 jenci1990 · Dec 05, 2014 at 07:35 PM 0
Share

http://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnLevelWasLoaded.html

avatar image fletch254 · Dec 06, 2014 at 12:21 PM 0
Share

This is great thanks, just what I needed.

avatar image
0

Answer by TSRajesh · Dec 05, 2014 at 07:23 PM

If we DontDestroyOnLoad(), we must also make sure that we don't have a second copy instantiated in the new scene, (And delete it if there is). (web search for singleton in unity).

But when we change scenes, the object's global postion will remain the same.. Which may or may not be appropriate for the new scene. What I do is, explicitly specify the starting location on the startup script of every scene.

Got the link.. http://unitypatterns.com/singletons/

Comment
Add comment · Show 2 · 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 fletch254 · Dec 06, 2014 at 12:22 PM 0
Share

Thank you for this. Your answer and jenci's answer together solve my problem but I can only choose one as an answer, sorry! This helped a lot though, thanks.

avatar image TSRajesh · Dec 06, 2014 at 05:16 PM 0
Share

You're welcome. No problems.. Really doesn't matter which answer is "correct".. We just share knowledge..

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

Scene loads in editor not in build 0 Answers

One Location Game (with Scenes Loaded and Disabled) 0 Answers

How do I fix lighting glitch between scenes? 1 Answer

I can't figure it out. 1 Answer

Attempting to Create an Essential SceneElements Checker 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