I was wondering if anyone else has the same problem and if there is a way to fix it. We’re in the final stage of our iPhone game development and we have some pretty big objects/prefabs with 2 or 3 different scripts with many variables attached along with mesh, sound emitter, colliders and a hierarchy of other objects underneath. The problem appeared gradually as objets grew: selecting one of these objects in the hierarchy or prefab in the project pane can now take as long as 20 seconds even on recent macbook pro models. We use Unity iPhone 1.0.3, I don’t know if the problem occurs on other Unity versions as we haven’t used them before. The objects work as fast as intended when the engine is running, but editing levels has become a slow process…
Thanks for your help,
PS I submitted a bug regarding this but it was closed without explanation, is it because the Unity team is already aware of this problem?
When you say big, can you quantify this? Open the Stats pane and report back on the the number of triangles, vertices in the scene. Also, can you report back on the size of the assets folder in the project directory and comment on whether you did any cutting and pasting of assets between projects. Finally, open up the activity monitor and take a look at the Unity process and report back on CPU utilization, memory and the number of threads. Also, look to see if there is another OS process that spikes in terms of CPU/memory utilization while prefab is being selected in the editor.
While we tend to keep prefabs small and use scenes to build complexity through composition, I have scene some pretty large prefabs loading in other people’s projects without issue.
Ok, the infos are below, but let me just point out first that playing the game is fast, no problem there, it’s only the selection of a big objet or prefab to show its information in the inspector pane that takes long.
Stats:
Draw calls 58
Tris 3.7 k
Verts 4.2k
Assets folder: 171 MB
We did not paste assets from another project.
When I select a large prefab Unity process goes up to 99% CPU usage (not responding) and spindump to 12% (2 cores). Memory does not spike for any process.
When I mean a big object, one of the culprits has 4 scripts attached to it with a combined total of 218 variables (including private vars) of many types. It has also an audio emitter. Attached to it are 7 simpler objects containing the different meshes necessary, some with scripts and some contain only a transform. The scripts are all written in javascript.
An object that takes only 2 to 4 seconds to show up in the inspector contains just 2 scripts with a total of 16 vars (most of which are Transforms, GameObjects and Script types) and no other object attached to it.
An object that takes less than 0.25 second to show up contains a mesh, a mesh renderer a script with 5 vars (int, floats and booleans only), a collider and a material.
It looks that some variable types create a problem, am I right?
Can you comment on the types and number of the 218 variables? Have you assigned object instances to these variables in the Inspector or are they empty?
When selecting a script file in the project pane, the bug also happens. The bug is caused by variables such as:
private var scriptRef1 : Script1;
private var scriptRef2 : Script2;
private var scriptRef3 : Script3;
By adding many of those script type variables to many scripts, each referring to the other (as we gradually did as our project grew), the problem occurs clearly.
I am including a dummy test project that highlights the problem (simply click on a script file, script1.js for instance in the project pane and enjoy the spinning beach ball for a few seconds.)
I don’t want to be mean, but if they refer to each other, you should take your design back to the drawing board. You are actively fighting against the idea of independent losely coupled components by introducing hard dependencies.
But at least it explains your problems. You create an infinite hierarchy and you can actually be happy that it takes seconds and does not crash due to your infinite loop (Unity assumes that stuff ends and waits a bit till it cuts it)
I don’t know that this is a bug. Each of your script contains a variable to every other script which means the engine has to bootstrap instantiation and reference setting across the entire set. A simple work around would be to have one centralized controller object/script which contains a reference to each unique script. When a script needs a reference to another it can simple refer to the reference held by the centralized controller. This would make instantiation and reference setting trivially straightforward.
First thanks for your help, but my dummy project is really a dummy one, in my real project I do have a centralized object called sceneManager that handles the references to the different objects in the scene. The presence of the references to the other scripts are explained by this:
In the sceneManager, I have say:
var object1 : GameObject;
var object2 : GameObject;
Say in object2 I have an OnTrigger func that needs to tell object1 to do something.
In order not to do a getComponent each time object2 needs to find the correct script reference to talk to object 1, I do in object2:
private var object1 : GameObject;
private var object1Script : Object1Script;
function Awake() {
sceneManager = GameObject.FindWithTag("SceneManager");
sceneManagerScript = sceneManager.GetComponent(SceneScript);
object1 = sceneManagerScript.object1;
object1Script = object1.GetComponent(Object1Script);
}
And then, when I need to tell object1 something I use object1Script.SomeObj1Function();
I guess it would be better to get the script reference in the sceneManager and use it like this in object2:
I still think it is an editor bug, an avoidable one by the user to be sure, but perhaps the editor could avoid going inside each referenced script file…
What you describe in your latest post is the optimal way to go. There is slight overhead in using accessor functions against private variables on your scene manager as opposed to using public variables directly, but you address the key issue of not looking up the object and component references every time you need to use one.
Given this, I don’t see an issue. We do the same within some very complex scenes and I have not seen this lag at all. Makes me suspicious that your installation has been corrupted in some manner. You might want to try re-installing Unity or rolling back in TimeMachine to an earlier date on the Unity folders before the problem started to appear.
You might want to submit your actual game project to Unity and let them take a look. I had a problem once with an intermittent crash and couldn’t track it down for the life of me. I sent them the whole game project and they got back to me in a day with the fix (a collider buried deep in a scene needed to be reset). They’re usually quite responsive.
Well, the two artists on the team reported that they experienced the same problem on their machines… I would be curious to see if others experience the same delays when loading objects in the editor in the test project I posted earlier.
I agree the accessor functions come with an overhead, but I find that they are more convenient in the long run (easier to debug, easier to control what happens to a variable…)
If they are seeing the issue, too, then it is unlikely the Unity install. I would definitely ship the game project off to Unity. Given that Nicholas has expressed an interest in looking into the problem, I bet you’ll see a pretty immediately response.
I personally think the overhead of the accessors is worth the benefit. If you use naked public variables it makes it difficult to track down cases where someone is changing a value unintentionally. It also makes it difficult to drive behavior off variable state changes without polling (e.g. incrementing the number of player lives remaining when the score exceeds some threshold, etc). The overhead is pales in comparison to the benefit.
The only times I use naked public variables are for variables I want to set in the editor, or if I need to read a volatile state variable from inside an Update or FixedUpdate function. There the overhead of the function call can become significant. But those cases are pretty rare since I try to take a very OO approach to designing my scripts in the first place and try to ensure that state variables that are needed inside an Update function are themselves state variables of the script/class.
I removed most of the script type variables in my scripts and used the scene manager to store the references as suggested and now the editor responds quickly for objects that were slow to show up in the editor. The problem really was because many of my scripts can talk to each other and thus as explained before I used private vars inside each of them to store the reference to each other ofter having found out their gameobjects throuh a central scene manager. The delay created in the editor became large because script1 had a reference for script2 which had a reference for script3 and 4 both of which had a reference to script1 and 2 and so on. It seems the editor had to go through all the branches thus created which took exponential time.
That’s good news. Indeed, doing the bootstrap-based instantiation and reference setting can be time consuming as you could imagine vtables being built incrementally in memory, etc. Glad to hear you got it worked out.
I saw this problem, too. I will grant that it’s largely an issue of class structure, but the whatever part of Unity is tracing these references should handle it better than seizing up if it finds an infinite loop in the references.
Hm. Do I understand this right - in the editor, Unity follows private references to other objects? Or are we talking public references? (because in the example they’re private)
Afaik, at edit time, private references stay unassigned. And then, even if it does, why would it follow further than one reference? Where is the point? Also it seems to me as if this is something that can easily happen to a user - so why does Unity not keep check on where it has been already, avoiding infinite loops in the first place?