Does Unity's Javascript have WeakReferences?

Hi There,

My goal is to create a cache of weak references, such that I can store and reference objects in the cache at will and if the objects are ever dereferenced elsewhere, the cache will not stop the garbage collector from purging them from memory.

There are at least threads threads in this forum which bring up mentions of WeakReferences: (the first is directly on point)

http://forum.unity3d.com/threads/63206-Garbage-collection-and-hashtable
http://forum.unity3d.com/threads/99176-static-vars-that-gets-destroyed?highlight=WeakReference
http://forum.unity3d.com/threads/116384-Finalize-function-called-from-separate-thread-How-to-deal-with-(C-)?highlight=WeakReference

I found the WeakReferences Class reference for .NET 2 at WeakReference Class (System) | Microsoft Learn

I tried " new WeakReference (whatever.gameObject)" but the compiler complains it doesn’t know the identifier WeakReference. Does anyone know if WeakReference is available for JS users or if there is another solution for a cache of weak referenced objects?

It’s System.WeakReference. Import the System namespace or write out the full path.

–Eric

Thanks Eric5h5! You’re amazing. I really appreciate it.

For future reference, is all of the API from the .NET 2.0 framework incorporated into Unity’s Javascript?

Thanks,
~Nick.

No, there are some specific things like System.Windows, System.Drawing, and so on that aren’t included. Aside from those, the version of Mono included with Unity is more or less equivalent to .NET 3.5. Note that it’s not really a language-specific thing; all languages in Unity use Mono so they all have access to the same classes.

–Eric

Thanks again Eric!
~Nick.