• 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 TheMannyzaur · Sep 28, 2016 at 12:14 PM · c#positiondontdestroyonload

DontDestroyOnLoad() not working?

Hi, so I made a 'reload' script :P that happens when the player collides with the enemy in that level. "level_x" been the level number

void OnTriggerEnter2D(Collider2D other) { Application.LoadLevel("level_x"); }

Now when this happens the enemy guy who is patrolling is restarted again. I dont want that so I created another script

 void Awake()
 {
      DontDestroyOnLoad(this.gamObject);
 }

and attached it to enemy. But when Player collides with enemy, enemy gets created twice with one in his current pos and the other in his starting pos which exits Playmode.

So my question is can someone please help me create a script the contains all these? Thanks in advance!

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

1 Reply

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

Answer by ScaniX · Sep 28, 2016 at 12:24 PM

There are probably other ways to reset your level than to reload the whole thing, but if you want to do that and keep dynamic elements from the previous instance:

1) Call OnDestroyOnLoad() on the objects you want to keep, like you are already doing. This is working as intended, because it just tells the engine to keep this object when a new scene is loaded. That way you end up with another enemy on each load.

2) To avoid the enemies to multiply, you need to discard additional instances. Normally you would do that by using a singleton pattern in which you assign your instance to a static member variable and immediately destroy any additional object in the awake method if it is not the first one (instance variable already set) or otherwise assign this instance to the variable.

In this case this does not seem like a good way, because you will probably have many objects with the same script, so you should instead create a global variable in one of your singleton scripts that tells the game objects that the current scene is a reload and not a fresh level. In the following example I just added it to the enemy script, because I don't know what other scripts you have.

 public class EnemyScript : MonoBehaviour {
     public static bool sceneReloaded;
  
     void Awake() { // in every dynamic object that you want to keep
         if (sceneReloaded) {
             DestroyObject(gameObject);
             return;
         }
         DontDestroyOnLoad(gamObject);
     }
 }


 // your collision reset thingy
 void OnTriggerEnter2D(Collider2D other) { 
     EnemyScript.sceneReloaded = true;
     Application.LoadLevel("level_x"); 
 }
 
 And finally, wherever you switch to the new level or restart the game:
 EnemyScript.sceneReloaded = false;
 // restart game/load new level here
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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

I am wondering how to save a int in the background 2 Answers

Raycast positioning problem 1 Answer

error CS1061: Type `UnityEngine.LineRenderer' does not contain a definition for `numPositions' and no extension method `numPositions' of type `UnityEngine.LineRenderer' could be found 1 Answer

Aligning sticky cubes 1 Answer

Position of one object relative to another 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