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
0
Question by chrille · Aug 03, 2014 at 11:26 PM · destroydontdestroyonloadgamecontrollerdestroyimmediate

is it OK to use DestroyImmediate in game?

Hi!

Right now i'm using DestroyImmediate to destroy duplicates of a game controller (which is a singleton) that has been set to DontDestroyOnLoad(). The reason why i need DestroyImmediate is that otherwise the Awake() and Start() functions of the duplicate scripts will run, which in my case is very time consuming. The reason for having duplicates of the game controller is that i otherwise would have to run the game from the start scene everytime just to test a certain level.

According to the reference it's not recommended though, but is it still valid?

Comment
Add comment · Show 4
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 Kiwasi · Aug 04, 2014 at 12:22 AM 1
Share

The main reason DestroyImmediate is recommended against is because it can destroy assets. Call it on a prefab and that prefab will be gone forever. Use with caution.

avatar image chrille · Aug 04, 2014 at 09:25 AM 0
Share

That does sound kinda scary. $$anonymous$$y gamecontroller actually is a prefab, but it doesn't get removed forever or anything like that. So calling it on a gameobject which in turn is a prefab is not dangerous i guess?

avatar image Kiwasi · Aug 04, 2014 at 09:39 AM 0
Share

Fine to call on a instance in the scene. Not cool to call on a prefab in the assets folder.

DestroyImmediate was originally designed for use in editor scripts, that can modify and remove assets. However you can use it in game, as indicated below.

avatar image meat5000 ♦ · Aug 04, 2014 at 04:03 PM 0
Share

it can destroy assets

Id totally forgotten about that. After learning it I remember drea$$anonymous$$g up a anti-hacker/anti-cracking system based on breaking the game install :D~

4 Replies

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

Answer by Bunny83 · Aug 03, 2014 at 11:49 PM

It is still valid and in your case i would recommend a different approach ;)

When using a start scene, it's better to use a simple script in each scene that checks if your singleton is there. If not it simply calls Application.LoadLevel. If your starting scene only contains the singletons, you can even do a Application.LoadLevelAdditive. It's in general not a good idea to have duplicates of a singleton. You can also create a dedicated scene for the singleton initialization. That could even be used in the access method ( property of the singleton. So when you want to access the singleton and it isn't there, you simply load your singleton scene additively.

Comment
Add comment · Show 2 · 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 Kiwasi · Aug 04, 2014 at 12:16 AM 0
Share

I've used the access method before. Psuedo code is something like this

 private static GameObject _instance
 public static GameObject instance {
     public get {
         if(_instance){
             return _instance;
         }
         _instance = Instantiate (prefab);
         return _instance;
     }
 }



avatar image chrille · Aug 04, 2014 at 12:18 AM 0
Share

Alright, thanks for the reply!:) The additive solution seems really neat. Think I'll try that one ins$$anonymous$$d. Thanks.

avatar image
1

Answer by meat5000 · Aug 03, 2014 at 11:35 PM

I use it. In my case I need instant destroy to avoid nullrefs.

http://answers.unity3d.com/questions/522400/destroygameobject-and-colliders.html

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 khuramtycoon · Aug 23, 2016 at 06:53 AM

hi sir i m new in unity i use destroyimmediate function my object is destroy how i come back it ?? help me please

Comment
Add comment · Show 1 · 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 tanoshimi · Aug 23, 2016 at 06:56 AM 0
Share

Please don't ask questions using the "Answer" button. Use the "Ask a question" button for that. As previously stated, DestroyImmediate destroys assets permanently and immediately - they can't be recovered. https://docs.unity3d.com/ScriptReference/Object.DestroyImmediate.html

avatar image
0

Answer by _eternal · Sep 07, 2018 at 03:23 PM

Late answer for the people who might come across this while searching like I did:

If you don't want to mess with your scenes or level loading code, just do something like this.

 void Awake()
     {
         if (currentInstance)
         {
             Destroy(gameObject);
             gameObject.SetActive(false);
             return;
         }
         else
         {
             DontDestroyOnLoad(gameObject);
             currentInstance = this;
         }
     }

Destroy completes at the end of the current frame, but if you just disable the object and stop execution of Awake, you can avoid some of the drawbacks without risking DestroyImmediate. My issue was that other objects were finding the duplicate via FindObjectOfType, so setting it to inactive worked for me.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make a saved score destroy on load new scene 0 Answers

Object.DontDestroyOnLoad ? 2 Answers

Destroy Object when scene loads 1 Answer

DontDestroyOnLoad for a specific scene 1 Answer

using Contains(gameObject) to find and destroy a gameObject from a list 2 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