x


Subscribe to an event when the application is loaded

Can I subscribe to an event from scripts who aren't in the initial scene when the game is loaded? Because I have an event called OnLanguageChanged and I need to subscribe to this event from some scripts who aren't called when the game is initialized, such as main menu, options menu, etc.

Is there a way to do this?

more ▼

asked Jun 21 '12 at 08:58 PM

GutoThomas gravatar image

GutoThomas
593 32 46 47

Scripts and objects that are not in the currently loaded level do not exist, and are inaccessible.

An exception are objects explicitly made persistent over scene changes, using DontDestroyOnLoad() (but the scene where these are declared have to be loaded at least once, before they become available), and static class members.

Jun 22 '12 at 12:33 AM Wolfram
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

If the object with the script / class that contains the event is loaded in a different scene, you have to make sure it isn't destroyed when you change the scene. Usually everything is destroyed when you load a level. In other words you need to use DontDestroyOnLoad on that gameobject. If you load another scene, you can access the gameobject like any other gameobject.

You can:

more ▼

answered Jun 22 '12 at 12:04 AM

Bunny83 gravatar image

Bunny83
45.5k 11 49 207

Thanks for both answers! Solved my problem! :)

Jun 22 '12 at 12:32 AM GutoThomas
(comments are locked)
10|3000 characters needed characters left

Not sure I understand your question correctly, but if your script providing the event is a persistent script (which it would need to be, using DontDestroyOnLoad()) in your initial scene, and the scripts that need to register to it are not in your initial scene, then just have them register to the event in their OnLevelWasLoaded().

If I misunderstood, please explain the situation more clearly.

more ▼

answered Jun 22 '12 at 12:05 AM

Wolfram gravatar image

Wolfram
9k 8 20 52

(comments are locked)
10|3000 characters needed characters left

If the object containing this event isn't instantiated, you can't subscribe to that event. You can use a static one though.

more ▼

answered Jun 21 '12 at 09:05 PM

Berenger gravatar image

Berenger
11k 12 19 53

Actually the script which contains the Event is called in the first scene, but the subscribers are in other scenes and I don't know how to subscribe them to this event.

Jun 21 '12 at 09:11 PM GutoThomas

Anyone with another idea?

Jun 21 '12 at 11:50 PM GutoThomas

As @Berenger says - you would need to use statics in them so that they could be initialized. It would help to know a little about what you are trying to achieve - it's hard to understand how something that isn't loaded needs to know that the app is loaded :)

Jun 21 '12 at 11:59 PM whydoidoit
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x47
x10

asked: Jun 21 '12 at 08:58 PM

Seen: 402 times

Last Updated: Jun 22 '12 at 12:33 AM