Okay so this might be a very simple thing to ask, but please bear with me…
Okay, so I created a script that contains a number of variables. I then placed this script inside an object I turned into a prefab.
I load this prefab via the Resources.Load() command and change the variables during the course of the program. When I run the program again at a later date, I find that all my variables have retained the changed values.
I attribute this to the fact that I don’t instantiate the prefab, but rather load the prefab. So instead of creating a copy, I just use the actual prefab. Wierd but true.
So now this brings me to the next question:
Let us say I have 10 images in a Resources folder. They don’t get loaded until I call for them and at that point I use the original, not an instance of it. So, if I say:
var tex : Texture2D;
tex = Resources.Load("image1",Texture2D);
tex = Resources.Load("image2",Texture2D);
tex will obviously point to image2, but what happened to image1?
Is it still loaded? Is it automatically unloaded once the reference to it was broken? How do I unload an item I loaded but didn’t instantiate?
Is it simply handled by the garbage collector or what? Surely it cannot be destroyed because it is the original image… Is the allocated memory simply freed…?
lehk, you are mistaken. GameObject.Find only finds GameObjects, and it also only finds objects instantiated in the scene.
As I understand it Mono has automatic garbage collection. So as soon as no variables are referencing an object, the memory should in theory be automatically freed.
this is how i understood it also, but since this is not an instantiated object that can be destroyed, but rather a resource that has to remain in the game…
I am under the impression that resource.load simply points to the disc image, and doesn’t actually load anything into memory. In essence, a resources folder becomes a scratch disc…
you’r pretty mistaken if you think that unity removes it just because there is garbage collection.
Also, Resources.Load indeed points to the resource, instead of returning a new instance of the resource you requested. (see the instantiate function in the examples)
i’ll do some extra research later. the points are:
is the resources folder loaded into memory on startup ?
are the requested assets loaded into memory when requested through load ?
if point 2 is true, are they collected when reference is lost ?