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 Eagle-Bound · Jun 15, 2016 at 06:15 AM · spritenoobduplicatescene change

removing player duplicates after scene transition

Whenever my player switches between scenes, like a house to the outside, my player is duplicated. So, I added these lines of code.

 private static bool playerExists;
 
 void Start () {
 if (playerExists) {
             Destroy(gameObject);
         } else {
             playerExists = true;
             DontDestroyOnLoad(transform.gameObject);
         }
     }

This does remove the player, but it removes both players, the original and the duplicate. If anyone knows why this happens and how I can fix my problem, I would really appreciate it.

Or, if anyone knows how to remove player duplicates after a player changes scenes, that would work too. My player controller script - http://pastebin.com/S3LdQa8S and my load new scene script - http://pastebin.com/1zegHvj1

Thanks!

Comment
Add comment · Show 2
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 cdnDave · Jun 15, 2016 at 09:03 PM 0
Share

If both your scenes contain a player, why are you setting it to no be destroyed when you transition scenes (DontDestroyOnLoad)? Seems like the easiest way to fix this is to just allow the player to be destroyed with the scene.

avatar image Eagle-Bound cdnDave · Jun 15, 2016 at 09:18 PM 0
Share

Thanks for your comment! It seems like it might work, but as I'm new to Unity, could you explain that process in simple terms for me?

2 Replies

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

Answer by TBruce · Jun 15, 2016 at 09:54 PM

Remove the whole

 if (playerExists) {
     Destroy(gameObject);
 } else {
     playerExists = true;
     DontDestroyOnLoad(transform.gameObject);
 }

thing.

Why do you feel you need it. If it is to have your data carry over from scene to scene then you should consider using PlayerPrefs (or a similar alternative) to save and restore the data.

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

Answer by cdnDave · Jun 15, 2016 at 09:39 PM

Posted this as answer but apparently it's waiting for a moderator to approve it...

When changing scenes in Unity the default behaviour is for all objects in the current scene to be destroyed before the next scene is created. By calling DontDestroyOnLoad(transform.gameObject); you are telling unity not to destroy the player when changing scenes. If your second scene also has a player object in it then you will now have two players, one from the first scene that never got destroyed and the one that's in the second scene.

I would try removing that call and letting the player be destroyed when you change scenes unless there's a particular reason you need that player to persist across scenes. Essentially I'd remove this whole block.

   if (playerExists) {
             Destroy(gameObject);
         } else {
             playerExists = true;
             DontDestroyOnLoad(transform.gameObject);
         }
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

Application.loadlevel adding double scripts to objects 0 Answers

Solid Object in 2D 1 Answer

How I can change the sprite of the image because the code doesn't work? 1 Answer

Strangely Transparent Sprites 0 Answers

Scale Sprite like UI Canvas 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