• 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 GutoThomas · Jun 21, 2012 at 08:58 PM · eventsdelegates

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?

Comment
Add comment · Show 1
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 Wolfram · Jun 22, 2012 at 12:33 AM 0
Share

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.

3 Replies

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

Answer by Bunny83 · Jun 22, 2012 at 12:04 AM

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:

  • search for it by name or tag.

  • use a singleton pattern so you can easily access it.

  • Use FindObjectOfType() to directly get the script if there is only one instance of it.

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 GutoThomas · Jun 22, 2012 at 12:32 AM 0
Share

Thanks for both answers! Solved my problem! :)

avatar image
2

Answer by Wolfram · Jun 22, 2012 at 12:05 AM

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.

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
1

Answer by Berenger · Jun 21, 2012 at 09:05 PM

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

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 GutoThomas · Jun 21, 2012 at 09:11 PM 0
Share

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.

avatar image GutoThomas · Jun 21, 2012 at 11:50 PM 0
Share

Anyone with another idea?

avatar image whydoidoit · Jun 21, 2012 at 11:59 PM 1
Share

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 :)

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

Unity Events, Listeners, Delegates, etc. 1 Answer

c# delegates/events using from js? 1 Answer

Hooks for updating keys in Animation Curve editor window 0 Answers

Update() VS delegate events 2 Answers

How can I use a delegate/event with enums and switch statement to switch powerup? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges