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 chillypacman · May 16, 2012 at 11:57 AM · instancesingletonawake

'Awake' not called first after scripts are recompiled during execution

I have an 'awake' function which sets up an instance, like so:

 public static instance MySingleton
 {
 get;
 private set;
 }

 void Awake()
     {
         Instance = this;
     }

This works fine, no problems.

However if I change a script during run time and unity does its recompilation 'Awake' isn't called on 'MySingleton' and another script from its normal update routine attempts to access it causing a null reference exception...

Any idea how I could make sure 'awake' is called straight after a script is recompiled or does Unity not provide a way to handle this?

Comment
Add comment · Show 3
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 flamy · May 16, 2012 at 12:00 PM 0
Share

you can stop the execution and run it again. what is the problem in it?

avatar image chillypacman · May 16, 2012 at 12:04 PM 0
Share

the problem is I'd like to be able to take advantage of Unity's ability to dynamically compile scripts at run time.

avatar image Kryptos · May 16, 2012 at 02:53 PM 0
Share

Dynamic compilation is broken. In my experience it always raise some exception.

1 Reply

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

Answer by Berenger · May 16, 2012 at 03:21 PM

Dynamic compilation isn't really a good idea, but that code is dangerous anyway. You should call an Init function both in get and Awake if the instance is null, that way it's initialize not matter which one is called first.

I added a simple way to have singleton with monobehaviour to the wiki if you're interested.

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 chillypacman · May 17, 2012 at 10:05 AM 0
Share

ah yes that code works perfectly, thank you so much for taking the time to put it in the wiki!

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

Is it costly to get a singleton instance each frame ? 1 Answer

Would setting this static cause a memory leak? 1 Answer

Singleton instance accessed in coroutine always null 1 Answer

Awake Method Called Twice 1 Answer

singleton becomes null? 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