• 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
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 t$$anonymous$$s 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 t$$anonymous$$s?

Comment

People who like this

0 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
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 everyt$$anonymous$$ng 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
whydoidoit

People who like this

1 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

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 (w$$anonymous$$ch 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
GutoThomas
whydoidoit

People who like this

2 Show 0 · 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

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

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

Comment
Bunny83

People who like this

1 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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

Question about unsubscribing eventhandlers OnDisable and OnEnable 2 Answers

How to unsubscribe from a delegate that is inside of a singleton? 2 Answers

Making Multiple Switches Affect Different GameObjects 0 Answers

Method doesn't get implemented after subscribing to Func 1 Answer

Is there any event or delegate that is called with OnValidate() ? 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