1

I Need to apply DontDestroyOnLoad on Scene.is it possible?

I Need to Do not disturb the scene when going in to another scenes also.here i'm sending mail,when ever clicking send button its going to authentication in mail server in this time my scene idle means not responding anything until come back to the response from mail server,so on that time i show one loading bar in my scene.this is not do process.the entire scene is hang until came response from mail server,so how to solve this?

 void  Awake()
{
      DontDestroyOnLoad(this.gameObject);
}
rene
  • 41,474
  • 78
  • 114
  • 152
user2594254
  • 27
  • 1
  • 4
  • The method you used is for making an object persist when moving between scenes. As per the reply below you must specify the gameObject, because 'this' refers to the instance of the script you're writing in. You should look up threading / async features of Unity if you want to keep our UI responsive when doing things. – stevepkr84 Aug 20 '13 at 07:36
  • i tried thats also but not working – user2594254 Aug 20 '13 at 07:39
  • 1
    If the locking happens when calling your mail server, we need to see that code. It seems doubtful the problem is directly related to `DontDestroyOnLoad` – Jerdak Aug 20 '13 at 14:00
  • 1
    `DontDestroyOnLoad()` should be safe, unless you are protecting something that relies on another object that does indeed get destroyed. –  Nov 11 '13 at 13:49

4 Answers4

4

After reading so many non-answers I finally found the answer in a Unity forum. DontDestroyOnLoad ONLY works if the game object in question is at a "root level" meaning right under the scene, not nested under any other object. This is not mentioned anywhere in the documentation.

pete
  • 1,878
  • 2
  • 23
  • 43
  • 3
    3 hours later, this is my answer. The unity 2020.3 documentation DOES NOT mention this, but this is how it works. – tfcmad May 06 '22 at 12:50
1

When loading a new level, scene, all Game Objects of the previous scene are destroyed by Unity.

If you want to protect a Game Object you can use the function.

DontDestroyOnLoad(gameObject);

It is important to note that when you say: this.gameObject you are pointing to pretty much the same thing, it just happens that this points directly to the script attached to that gameObject. So you don't need the this part, just gameObject will do.

Ideally you could protect that gameObject inside void Awake()

void Awake()
{
   DontDestroyOnLoad(gameObject);
}

The above code will prevent Unity from destroying that gameObject unless your game closes completely or at a later point you call Destroy() on it. That means you can change from scene to scene and the gameObject will survive. However, if you make it back to the scene that creates that gameObject you are protecting you may run into problems if you do not have the logic implemented that prevents you from protecting a second, third, or many of that gameObject.

Your second question, if I understand it correctly: You want to send mail when you change scenes, but your progress bar wont progress while changing scenes, it just stays there, static.

If that is the case then your problem is in Application.LoadLevel(sceneName); If you have the free version of Unity, then you need to come up with your own creative way of showing that progress bar, because Application.LoadLevel() will halt everything until it takes you to the new scene.

-1

I don't completely understand what you are saying. But because in your context, this in represents most probably a Monobehaviour, try the following:

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

or

void Awake() {
   DontDestroyOnLoad(gameObject);
}

See http://docs.unity3d.com/Documentation/ScriptReference/Object.DontDestroyOnLoad.html

jonas
  • 531
  • 4
  • 15
-2

I recommend you use a coroutine, with the 'yield' statement check out this documentation of the WWW class, which likewise involves writing code to cope with waiting for a response from the web, without hanging your Unity3d program

coroutines are pretty powerful if you're working with tasks that take more than a frame or two. Richard Fine (AltDevBlog) has posted a really detailed description of what they are and how to use them, which I thoroughly recommend.

Matt Smith
  • 91
  • 1
  • 4
  • when ever clicking submit button the scene goes to hang state so using coroutine is also not use. – user2594254 Aug 21 '13 at 09:31
  • if you post up some of your actual code, perhaps we can help you get it working. At present it's not very clear exactly what you're doing - i.e. why you need to change scene while waiting for response from mail server. By the way [here is a SO example of mail sending in unity](http://stackoverflow.com/questions/4148019/authentication-or-decryption-has-failed-when-sending-mail-to-gmail-using-ssl) – Matt Smith Nov 03 '13 at 21:49