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
1
Question by Heke · Jun 26, 2013 at 02:41 PM · objectscenedestroymultiple

How to prevent objects instantiating between scenes?

I use DontDestroyOnLoad(this); on one of my object and when going back and forth between scenes, it spawns multiple same objects because they never destroy. I want that there would be only one object. Is there some particular code to prevent object from spawning at the start of certain scene?

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

4 Replies

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

Answer by Wuzseen · Jun 26, 2013 at 02:54 PM

This is a problem with several solutions.

The easiest and most intuitive is to instantiate your dontdestroyonload objects in a scene before the one where they are actively used. That is, you'll have a "loading" scene. Then you add some logic on your dontdestroy objects that will destroy themselves when they need to. You can add a flag to an object as well to do similar things. The important thing that makes this work is that you don't go to the loading scene a second time.

This works and will most often get the job done for things like audio. But sometimes you have to go deeper depending on the situation.

The next most common way I tackle this issue is to have an object in my scene that handles instantiating my dont destroy on loaded objects by checking if they already exist or not.

     if(GameObject.Find("levelObj(Clone)") == null) {
         Instantiate (levelObj);    
     }

I use the above code to do this.

levelObj is an object that has dontdestroyonload in it.

This script resides on a separate object that isn't destroyed on load, "levelObj" is a public variable for this code that I define via the inspector.

This script checks if the dontdestroy object already exists. If it already does, don't create a new one.

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 Heke · Jun 26, 2013 at 03:08 PM 0
Share

Damnit you are genious. I was thinking about to make a zero level but didn't want to because of laziness and I thought that would mess everything up. But I'll try the 2nd method, that should work I believe.

PS Now I know why there are those "press strart to begin" -screens lol.

avatar image Wuzseen · Jun 26, 2013 at 03:13 PM 0
Share

I had that same realization a month or two after starting using Unity. It's not really the same idea for every game, but for how unity is used it is a natural use for that sort of screen.

$$anonymous$$eep in $$anonymous$$d you can just have an empty scene where this happens, it will load the next scene really fast--before you even know it, really.

Don't forget the second solution I posted. It's far more useful as you don't have to swap between scenes while editing which can put strain on your workflow.

avatar image lockay · Apr 30, 2016 at 11:00 PM 0
Share

Thank you @Wuzeen.

I used your first strategy, creating another scene for loading. I deleted everything in the scene and added an empty gameobject with this script. Objects is an array where you can add any object that should be instantiated. Then, a for loop runs and instantiates them and says not to destroy them. After that, the first scene is loaded, which you can set with the startScene variable.

 #pragma strict
 
 var objects : GameObject[];
 var startScene : String;
 
 function Start () {
     for(var i : int = 0; i < objects.length; i++){
         Instantiate(objects[i]);
         DontDestroyOnLoad(objects[i]);
     }
     Application.LoadLevel(startScene);
 }
avatar image
0

Answer by AVividLight · Jun 26, 2013 at 02:55 PM

Hey Heke,

I believe the problem you're having is that somewhere in your code you instantiate the object, without checking if it already exists. There are many ways you could fix this, and using a variable would probably be easiest. You could also give your object a tag, and check if the tag exists before instantiating.

I hope I've helped. - Gibson

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
0

Answer by keni4 · Mar 17, 2017 at 03:55 PM

@Heke, Use Singleton pattern for this object. Simply, destroy object on Start or Awake callbacks if instance of this object already exist.

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
0

Answer by Joe-Censored · Mar 17, 2017 at 08:36 PM

I give the object a unique tag, then on scene load do a FindObjectWithTag, if no object is found I instantiate the object, otherwise I don't.

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

19 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

Related Questions

How do I keep objects destroyed between scenes? 1 Answer

Destroy all objects in the scene? 0 Answers

Destroying object using his name and raycast 2 Answers

Destroy object on button push? 2 Answers

Recover Object After Destroyed 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