Parenting

Greetings,

I am experiencing heavy lagging issue when I am parrenting my instantiated objects, and I do not know why or what I can do too reduce it.

I guess I might need to parrent them in some other way, but after countless of hours trying to figure that out from google I now need all the help I can get.

I need to say though, that I am instantiating alot of objects on very short time, but if I do not parrent the objects, I don’t only lose the lagg but I can instantiate up to probebly 3-4x of the previosly ammount whitout any lagg. So, this obviosly tells me that the lagg is not the ammount of instantiatied objects, but the fact that I am parrenting them!

I even tried to parrent them outside of the for loop, rather after the instantiation was done, but still the lagg is heavy even though I tried to limit the ammount of parrenting each seconds, etc!

It looks to me that parrenting is really taking alot of my computer even though I got a decent one!

GameObject myGAMEOBJECT = Instantiate(myGameObject_LIST[0], new Vector3(myX_CORDS, OnGeneratePerlinNoiseEvent(myX_CORDS, myZ_CORDS), myZ_CORDS), Quaternion.identity) as GameObject;
					myGAMEOBJECT.transform.parent = myROOT.transform;

So, that is the code, don’t mind most of it since everything works perfectly as it should do only the lagg ofc!

I really hope someone can help me out whit different method of doing this, since I need those object parrented for the rest of the system to work, since I need each chunk to be in its own object so that I can turn them off individually!

There might be another method for that too, rather then parrenting I mean, but should parrenting be that heavy?

Best Regards,
Winsj

Instantiation and component access are expensive. Often the only way to mitigate these hiccups is to do the work behind a loading screen, in a separate thread, or distribute the work over many frames. There’s nothing revealing about the code or the situation described that can be helped any other way. You could look into pre-instantiation, also called “pooling,” which is probably the most common technique for overcoming instantiation lag. It involves instantiating disabled objects (behind a loading screen or distributed over many frames), then enabling them when they’re needed. You can’t beat the lag, but you can choose when and where it happens.
Good luck!

edit:
I guess it’s worth mentioning that if you’re working with truly massive numbers of objects, the sheer number of them can cause a performance issue. If you’ve proven to yourself that the parenting process is definitely the issue, consider refactoring any code that depends on them all being children. Also consider reducing the number of objects in play, since you’d be surprised how little it takes to cause lag. And I guess you could consider having fewer children per object, since a large amount just plain takes longer to access.

not true at all.

Instantiate can be a little expensive on mobile, but that’s the only situation. Destroy on the other hand…

Accessing a component is also not expensive if done correctly.

As for the code above, there’s no reason for it to be causing any performance problems. I do parenting a lot without any performance impact.

I am 100% sure that the parrenting part of my code is the reason for the lagg issue, becuase like I said if I just insantiate them whitout the parrenting, I can easly instantiate over 8K objects in now time. It is when the parrenting comes in that I can only insantiate around 200 objects before it is unplayable!

Sadly, I cant pre-instantiate, becuase I am updating the world frequantly whit instantiating new objects and or remove old once (this part works perfectly smoot btw, it is just the parent thing that dont)!

Even compared to heavy arithmetic and data manipulation, instantiation and component access are quite expensive. Maybe it’s more prudent to say they are among Unity’s most costly atomic operations. That’s why Unity recommends pooling objects and caching GetComponent results rather than performing said requests on the fly. Naturally it makes very little difference when working with a small number of instantiations per frame, or when other factors aren’t at work, but it sounds like he’s doing a whole lot of creating and parenting at once if he’s noticing the lag. Optimization at this level is necessary when many objects are in play. That, or I really need a new computer! :slight_smile:

edit:
Just FYI, having anywhere near 8k of just about anything is a recipe for lag, let alone GameObjects with MeshRenderer components or heaven forbid Rigidbodies+Colliders and complex parent/child hierarchies. Each level adds Matrix multiplications and internal transform manipulations that will add up and impact performance.

You need to investigate ways of optimizing for that many objects at once. And pooling / pre-instantiating is always an option, and probably a necessary one to approach this number of objects with playable framerates. It just means you’ll have to refactor anything that depends on Start() or OnDestroy() and the like into OnEnable / OnDisable. Or distribute instantiation over many frames. Create a pooling and queuing system that does all this for you.

Yeah, I am not even rendering nor instantiating nearly 8K object at once, but I can whitout any lagg issue, so that proves the fact that there is no reason for me not to be able to instantiate atleast 250-500 objects at once and render maybe 1000 in the game at once, right? But the thing is, I can barly render 150 whitout my client crash when I am using the parrenting, so there is something wrong about it. I know I can use object list that will solve the same thing I need in the game, but only problame there is I cant generate lists on runtime whit desireded name!

I can ofc make pre-made lists, but the game world/map is infinite expanable, whit perlin noise and what not!

Agreed that 8k+ objects is gonna slow any system down…

What sort of objects are you instantiating? Is this some kind of minecraft type scenario?

I can easly do that.

I missed to say thought that the generation, whit or whitout the parrenting is spread out over some seconds so it is not that many each frames. But either way, I can whit my generation system generate 4-5x more objects to get the same ammount of lagg whitout the parrenting, as I do whit it.

I am for now only generating cubes, but it is not going to be any minecraft scenario when it is finnished really, but you can think of it like that, I just didnt want to mention it becuase usly that resoults in ppl pointing me to minecraft starterpack which has nothing to do whit what I am trying to develope!

EDIT:

But, I might be able to do it whit the use of list, becuase adding each instantiasion to a list each time the loop runs doesnt effect the performance at all really. I guess if thats what I should aim for I need some help figuring out how to make lists on runtime, I have tried to search for sometime but can’t seem to figure it out!

Of course it completely depends on the complexity of the objects you are instantiating…

If you dont actually need the parenting, work around it. Sometimes the best solution is to change your solution.

lists are pretty easy to use… just make sure you include System.Collections.Generic then define lists as per norm (C#)

Ofc, I know that it depends on the object, but that is not my problem in any way. I can’t jump into generating heavy objects before I even got a generation system that even remotly works for low outcome, that is to me to jump in whit the head first!

I don’t need to parent, and you are completly right about changing my solution to it, but even though I do know how to use lists I just want to figure out why parenting do not work for me. I don’t like to just run away to something else when I can’t get it to work, I want to learn from it.

I really can’t see why just by removing that last line of my code that you can see on my main post, results in the game going from 100% lagg to 0%!

Do the objects have rigidbodies? Without knowing more about what you are actually trying to parent I am guessing… My guess would be the performance problems you are seeing are physics based though.

There’s a note in documentation that a parent and child should not have rigidbodies. If you have pro, I would run the profiler to determine the cause.

No, my objects is just empty object whit simple mesh, so that should not be the reason for the lagg since usly instantiation on its own laggs if the prefabs are heavy, but in this case it doesnt.

for(int myINT_X = 0; myINT_X <= myCHUNK.x; myINT_X++)
			{
				for(int myINT_Z = 0; myINT_Z <= myCHUNK.z; myINT_Z++)
				{
					GameObject myGAMEOBJECT = Instantiate(myGameObject_LIST[0], new Vector3(myX_CORDS, OnGeneratePerlinNoiseEvent(myX_CORDS, myZ_CORDS), myZ_CORDS), Quaternion.identity) as GameObject;
myGAMEOBJECT.transform.parent = myROOT.transform;
				}
				yield return new WaitForSeconds(0.001F);

So, if you want to and can try this code, you can see if you experience the same lagg whit the parent code, as whitout. It would be very helpfull if someone could try it!