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 bogweasel · Nov 09, 2019 at 10:01 AM · referenceclasses

Im not sure why this works, Class referencing & memory question,

Hi,

Ive made the follwoing function to act as a quick load (test for now) as far I understand things the way ive written it shouldnt work, but it does. I'm hoping somone can explain why as I've obviosuly misunderstanding somthing.

 public void TESTLOAD()// test quick load
     {
         int test = 1;
         if (File.Exists(Application.dataPath + "/Maps/TESTSAVE" + test + ".save"))
         {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.dataPath + "/Maps/TESTSAVE" + test + ".save", FileMode.Open);
             SaveData saveFile = (SaveData)bf.Deserialize(file);
             file.Close();
 
 
             ZoneData zone = saveFile.world[0];// test
             int mapNumber = 0; //test
             int day = saveFile.day;
 
             ZoneControl.zoneControl.QuickLoad(zone, mapNumber, day);
 
             PlayerControl.playerControl.playerData = saveFile.playerData;// TEST will probably have to copy rather than refer
             UIControl.uiControl.UpdateInventory();
         }
         else Debug.Log("FILE NOT FOUND");
     }

Specifically

 PlayerControl.playerControl.playerData = saveFile.playerData;

I would have though saveFile.PlayerData stopped existing when the fucntion ended, causing playerData to be garbage.

But it seems to work fine.

Im confused.

Cheers Mark

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
1

Answer by Bunny83 · Nov 09, 2019 at 01:48 PM

Well it just works as it should :)


Objects, data, memory becomes garbage as soon as no other object is referencing the object anymore. From a pure C# perspective there are essentially two main root anchors:

  • static variables

  • local variables of the currentl< executing method and all of it's stacktrace.


In Unity there are some additional hidden "anchors". Of course the Unity engine itself has internal lists of objects. Specifically the current scene and it's gameobjects. Each gameobject has references to its components which will include your MonoBehaviour derived classes. So when you store a reference in a member variable of one of your components it is not being collected by the garbage collector since there is still a persistant reference to that object. The garbage collector can only collect objects which are not referenced by any of the "anchors" I've mentioned above.


Unity actually has many other hidden stored references. For example all objects derived from UnityEngine.Object are persistant since they are tracked / stored on the native C++ side and you can get back a managed reference though FindObjectsOfType. Those objects need to be destroyed through UnityEngine.Object.Destroy.


This line:

 SaveData saveFile = (SaveData)bf.Deserialize(file);

Creates a new object of type SaveData and the reference is stored in the local saveFile variable.

In this line:

 PlayerControl.playerControl.playerData = saveFile.playerData

you seem to store a sub object of your saveFile inside a member variable of another script. Since that other script is persistant once you store the reference in the member variable the object in playerData will stay in memory since it is still referenced even when your "TESTLOAD" methods ends.

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 bogweasel · Nov 09, 2019 at 06:23 PM 0
Share

Thats great thankyou. $$anonymous$$akes sense now :)

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

118 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 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 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 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 avatar image avatar image

Related Questions

How to pass over different scripts, subscribing to the same interface 0 Answers

Assigning class reference dynamically? 1 Answer

Instantiating a class using dragged-in references 0 Answers

How do I reference variables from other class files? 1 Answer

access gameobject from instance of class 1 Answer


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