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 /
avatar image
0
Question by SesshogeGames · Nov 28, 2018 at 09:11 PM · 2d gamerpgdontdestroyonloadduplicatescene-change

DontDestroyOnLoad duplicate prevention code is deleting the original player

Still working on a 2D overhead game. So now I realized that the script I am using for DontDestroyOnLoad is deleting the original player that I am using.

 DontDestroyOnLoad(transform.gameObject);
         if (GameObject.Find(gameObject.name)
                  && GameObject.Find(gameObject.name) != this.gameObject)
         {
             Destroy(GameObject.Find(gameObject.name));
         }

This is the code that I am currently using to delete duplicates, but when loading back to the original scene, it deletes the original and messes up the whole rest of the code, such as the start points I am using.

Does anyone know how to fix this?

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

Answer by GamitusLabs · Nov 28, 2018 at 10:38 PM

This is how to make it so your object is the only instance and is preserved between scenes in Unity, this is similar to the 'singleton' design pattern.

 public static GameManager instance = null;
 void Start ()
     {
         if (instance == null)
         {
             instance = this;
             DontDestroyOnLoad(gameObject);
         }
         else
             Destroy(gameObject);
 }
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 SesshogeGames · Nov 29, 2018 at 04:16 PM 0
Share

Does this require some setup code somewhere else?

avatar image SesshogeGames SesshogeGames · Nov 29, 2018 at 05:12 PM 0
Share

Also, where does Game$$anonymous$$anager come from? It's shooting me an error.

avatar image GamitusLabs SesshogeGames · Nov 29, 2018 at 05:33 PM 1
Share

$$anonymous$$y apologies,

Game$$anonymous$$anager is the class this is encapsulated in. So...

 public class Game$$anonymous$$anager : $$anonymous$$onoBehaviour
 {
 public static Game$$anonymous$$anager instance = null;
 
 void Start ()
      {
          if (instance == null)
          {
              instance = this;
              DontDestroyOnLoad(gameObject);
          }
          else
              Destroy(gameObject);
      }
  }

This method can be used on any class, just make sure that you truly only want one of them.

avatar image
1

Answer by ecv80 · Nov 28, 2018 at 10:26 PM

I'm not very sure about your conditional there. GameObject.Find() returns a GameObject or null. GameObject overloads bool (ain't C# weird) to return true if not null. So there's nothing wrong with that usage... but I guess that's not the comparison you intended. You are saying: "If this game object exists(true/false) and this (exact same) game object exists(true/false), compare true/false (which is the result of true/false && true/false) to this.gameObject(true/false).

Now let's examine the case where your gameObject exists and your this.gameObject exists: (true && true) [=true] != true. This will always be false, so your conditioned code won't run, nothing will be destroyed.

Now the case where your gameObject exists and your this.gameObject (unlikely, I presume, but not impossible) doesn't: (true && true) [=true] != false. This will always be true, so your conditioned code will run, and gameObject will be destroyed.

Now the case where your gameObject doesn't exist and your this.gameObject does exist: (false && false) [=false] != true. This will always be true, so your conditioned code will run, and gameObject will be destroyed. Note that in this case, the first part of the condition will only run as far as checking whether the "first" gameObject exists, ignoring the "second" one, to then check the result against the this.gameObject.

Now the case where your gameObject doesn't exist and your this.gameObject doesn't either: (false && false) [=false] != false. This will always be false, so your conditioned code won't run. Same applies regarding the first check in the first part of the condition.

So as far as I can tell, your code isn't behaving as you intended. You would need to find different ways of checking if two game objects are clones or whatnot. You could compare their names, or hopefully something more reliable, which unfortunately I do not know.

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

111 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

Related Questions

When die - reset score from the last scene 2 Answers

Best way to deal with DontDestroyOnLoad when returning back to the same scene? 1 Answer

How to duplicate several layers in a tilemap?,How to duplicate layered tilemaps? 0 Answers

NPC Party Member Direction 1 Answer

how to load a different save position in different scene? 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