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 PreciousRoy · Jan 08, 2018 at 04:43 PM · fpsdestroydelete

Player not deleting

Hello,

I am trying to get my scene changing working. but for some reason my player is not correctly deleted.

     private static bool Exists; 
 
     void Start()
     {
         anim = GetComponent<Animator>();
         body = GetComponent<Rigidbody2D>();
 
         if(!Exists)
         {
             Exists = true;
             DontDestroyOnLoad(transform.gameObject);
         }
         else
             Destroy(gameObject);
     }

but when i run switch levels i have 2 players where 1 of them has all of its element set to NULL and is also not visible in the hierarchy. so after switching maps 100 times i have a lot of objects that i dont want.

any idea what i am doing wrong?

Roy

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

Answer by MacDx · Jan 08, 2018 at 05:05 PM

DontDestroyOnLoad (line 11) is the root of your problem, just like the name says, this method marks an object so it doesn't get destroyed when loading a new scene (When changing scenes everything gets automatically destroyed, except of course for objects marked by DontDestroyOnLoading). That's what is not allowing the player to be correctly deleted when changing scenes, so every time you change scene, this player won't get destroyed and also there will be a new player spawned since that's the content of the new scene, and that player will also not get destroyed and so on. The answer is simple, get rid of that line of code. However the real question is, why did you use DontDestroyOnLoad() in the first place?

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 PreciousRoy · Jan 08, 2018 at 05:10 PM 0
Share

If i dont use DontDestroyOnLoad(transform.gameObject); wont it delete any of lets say my items and gold because it makes a new version of my player?

Edit: also i am not spawning a new player in my other map so when i go to that one without DontDestroyOnLoad i have no player and no camera

avatar image MacDx · Jan 08, 2018 at 05:42 PM 0
Share

@PreciousRoy please do not post an answer when you mean to comment or respond, use a comment ins$$anonymous$$d.

If i dont use DontDestroyOnLoad(transform.gameObject); wont it delete any of lets say my items and gold because it makes a new version of my player?

Yes changing scene will delete everything in the old scene, everything that is in memory that is.

also i am not spawning a new player in my other map so when i go to that one without DontDestroyOnLoad i have no player and no camera

But you probably are at least going back to the scene where the player first appears, and that's why you end up with duplicate players.

I understand now that the purpose of using DontDestroyOnLoad in your code is to keep information between scenes, information like your player gold, items, etc. However you are taking the wrong approach. If you want to save information like that, you should be using PlayerPrefs ins$$anonymous$$d, which is an easy way to save information to your disk and then loading said information later, when you change scenes or even after you closed the game and opened it again (which wouldn't be possible with an object that's just saved in memory). You should do some research on PlayerPrefs so I'll leave some useful links. Another way to save information is using serialization . Serialization is a more advanced technique than PlayerPrefs and will take you more time to make it work, however, it allows for a lot more flexibility since you can save information in any format you want. You will need to choose which approach is best for your case.

https://docs.unity3d.com/ScriptReference/PlayerPrefs.html

https://unity3d.com/learn/tutorials/topics/scripting/high-score-playerprefs

avatar image PreciousRoy MacDx · Jan 08, 2018 at 06:52 PM 0
Share

Thank you this makes a lot more sense. I am quite new and was following a tutorial and that was how they solved the problem.

avatar image
0

Answer by minimidimike · Jan 08, 2018 at 10:36 PM

If you are still looking to use this way, I use something similar. This is what I use (with Player being the Player gameObject). Also, use Awake() to make sure the existing one survives the scene change.

 public static Player player;
 void Awake(){
     if (player == null) {
         DontDestroyOnLoad (gameObject);
         player = this;
     } else if (player != this) {
         Destroy (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

154 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 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 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 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 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 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

Error with unity shooting script: Monster Death Script Doesent Work 0 Answers

How do I delete objects in an animation in a certain order? 1 Answer

Problem with instantiate and destroy multiple objects 0 Answers

How can i Delete original Keep Clone 1 Answer

Destroy question if its already asked 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