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 kalvinlyle · Jan 11, 2012 at 02:09 AM · memorygarbage collectionmemory leaksgc.collect

Memory Leak: Base Class Script duplication on load

The characters in our game are derived from a base class script, which is not attached to any object.

When a level is reloaded or a new level is loaded the character is destroyed, but the base class script remains and when the new character is created at the start of the next level an additional base class script is created.

I know this is happening because the score is incremented in the base class. When the score is increased by 100 in the first level, the score goes up by 100, if the same level is reloaded or the next level is loaded the same 100 point increase results in a 200 point increase. Each reload adds 100 to the score every time it is updated. The object that sends the score update only sends the message once, but there are multiple objects adding 100 to the score (one for each retry or reload)... I'm assuming this is because the Base Class script is being duplicated.

So my question is how can I clean these duplicate scripts from the scene?

I've tried Resources.UnloadUnusedAssets(), with no result, also GC.Collect()

I'm stumped...

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 by0log1c · Jan 12, 2012 at 05:44 AM 0
Share

It sounds a lot like what DontDestroyOnLoad() would do: add a script instance on every scene load. In any case it is not related to Resources, GC and I'm confident 'leaking base class' is actually impossible. Are you sure the character GameObject aren't piling up, too?

2 Replies

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

Answer by kalvinlyle · Mar 10, 2012 at 06:52 PM

I solved this by loading a empty scene between levels.

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 Kryptos · Jan 11, 2012 at 09:59 PM

You wrote:

but the base class script remains and when the new character is created at the start of the next level an additional base class script is created.

That does not make any sense. Your misunderstood the concept of base class and inheritance. When you use an instance of a script that inherits another script (base class) there is only one script instantiated.

IMHO the whole object is leaking between scene. This can happen if you have another script that is not destroyed between scene and that keeps a reference to your so-called 'leaking' script.

Make sure to release any references (and cross-references) before loading another scene.

OnDestroy() method which is called on every active component upon their destruction call be of some use in this case.

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 rabbitfang · Jan 12, 2012 at 05:09 AM 0
Share

Also, make sure that the score variable is not static, as that would cause the value to persist between levels (I think).

avatar image syclamoth · Jan 12, 2012 at 05:33 AM 0
Share

Yeah, I think it would, actually! I've never tested this, because I avoid static variables when I can, but this is actually another reason not to use them...

avatar image by0log1c · Jan 12, 2012 at 05:49 AM 0
Share

I believe the problem is duplicating script instance and not persistent data but yeah - testing shows a static variable is apparently persistent through scene loads and object destructions.

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

8 People are following this question.

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

Related Questions

relation between dereferencing and memory deletion 0 Answers

Used heap is above 10MB, should I worry? 0 Answers

Is there a way to tell what is being garbage collected? 1 Answer

Unity iOS controlling heap size growth 1 Answer

Releasing Unity Memory from an iOS App 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