Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 UniqueMAX · Aug 31, 2015 at 04:18 PM · referencedontdestroyonloadreference-other-objectstatic-vars

Restoring Static Variables after DontDestroyOnLoad

Hello everyone!

Since the day I realised I'd have to learn using DontDestroyOnLoad() I knew I'd mess up a lot of things. It seems to be one of the hardest Unity Scripting topics for me =(

So I have an object Alpha with multiple children in a scene 0 with other objects connected to Alpha via script references. Alpha has DontDestroyOnLoad check and also destroys duplicates in case scene 0 is reloaded in playmode more than once.

Every time I go to scene 1 and back to scene 0 I have all the objects on their places, but objects other than Alpha lose every reference to Alpha and his children. It is caused by the fact that newly created Alpha that belonged to reloaded scene 0 got destroyed. So I decided to change the script to opposite, destroying the old Alpha on reload.

The result was the opposite but still more preferable. After reloading scene 0 again all objects were on their places and all the references were completely fine. The problem is that the script attached to Alpha has static variables that become null for the reloaded Alpha if I destroy the old one.

Is there a way to restore those variables and keep the references untouched?

Comment
Add comment
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

1 Reply

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

Answer by Mikilo · Aug 31, 2015 at 04:36 PM

There is many way to handle what you are using. It is called the Singleton pattern.

Something that must be unique no matter what, and optionally stays alive for ever and ever.

But first of all, I must suggest you to avoid static. There is plenty of topics about it in the Internet, and enough reasons to make you avoid it.

But anyway, about your problem. You can use PlayerPref to store and restore values between scene. It is a pretty easy way to do it.

You restore values in Awake() and store values in OnDestroy().

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 UniqueMAX · Aug 31, 2015 at 05:02 PM 1
Share

Thank you very much! It worked perfectly!

avatar image Mikilo · Aug 31, 2015 at 05:04 PM 0
Share

It was intended XD

But you should really care about static.

From my point of view, you should not use static if you don't how it works internally, deeply in its guts! This advice will make you do better code!

Anyway glad to help.

avatar image UniqueMAX · Aug 31, 2015 at 05:09 PM 1
Share

Yes, I'll look it up totally, thank you =D

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

29 People are following this question.

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

Related Questions

Static calls to Static GameObject 2 Answers

Referencing another C# Script Error 1 Answer

Can't re-enable a gameobject 1 Answer

Changing the Variable of an objects script, through a different objects event. 1 Answer

How do I find which objects are referencing another? 10 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