- Home /
Instantiating prefab vs loading new scene
Hi,
I have a long linear level (kind of an endless runner). It is segmented into, well, sections (containing a series of jumps or puzzles). There are two ways I can handle this:
Make every section in its own scene. Since both player and camera DontDestroyOnLoad(), there are occasional hiccups (loading scene) but it is a continuous experience. Advantage: Scenes are easy to edit. Disadvantage: Loading scene is heavy operation.
Make the segments into a prefab each and put them all into one scene. They will be instantiated and destroyed as the player is onTriggerEnter() and onTriggerExit(), respectively. Note that the scenes from option 1. and the prefabs from option 2. contain the same objects, they are basically equivalent. Advantage: Maybe prefab instantiation / destruction are faster than loading scenes. Disadvantage: Inconvenient to edit; most prefabs must be erased from hierarchy before running since they are instantiated in runtime.
Which would you favour / recommend? I am inclined to 1. since scenes are easier to edit. Curious to hear your opinion.
Yes, but I actually want the sections that the player has beaten to be destroyed. They contain too many unrelated and computationally heavy objects like joints and (everyone - with - everyone type interactions). I want that gone for good when it's not needed.
Answer by Owen-Reynolds · Dec 31, 2012 at 01:01 AM
You could have the "to be yet seen" objects simply inactive. They would probably take very little memory -- possibly just as much as prefabs+spawning code. And, you could edit them right there and check them inactive when done. For ease, put each 50 foot section into a parent object.
For prefabs, the 50 foot section trick might help. Could edit each section normally. When done, drag them all into their prefabs and delete. The cool part is, dragging into the Heirachy pane will put them where they started (unlike dragging into the Scene, which picks a new position.) Spawning them could also key off the preFab position.
Prefabs with prefabs in them (like a long hallway with only prefab benches, lamps ... ) should still give all the space-saving advantages. For example, if section 12 is the same as section 5, could just drag the section 5 prefab into S12, with the new location (and some extra plants or something.) The section 12 prefab will only need a handful of bytes to store the location and the S5 link.
I thought about this but I have a problem here. An important part of my game is a static list to which an object is added on Start() and removed onDestroy(). If I have N such objects, every FixedUpdate() iteration checks N^2 interactions. The inactive objects would take up space in the list which is not good... There don't seem to be functions like onActivate or onDeactivate which would be helpful here...
That's why I was thinking about 'hard' things like destruction of a prefab or loading a new scene. This way, each prefab / scene contains a manageable number of objects at a time, so I keep few interactions at a time.
It's your list -- no reason you can't add/remove objects when you (de)/activate them, if you want. You may have to hand-add the ones at scene start (or pre-add everything with a certain tag.)
A compare all-to-all every frame? Unity does most of that for you, using tricks to run faster. Sure you can't get rid of that?
Your answer
Follow this Question
Related Questions
How can i transfer an object from scene1 and instantiate it in scene2? 2 Answers
Are the prefabs stored in RAM or in the Hard disk? 0 Answers
Instantiating in Awake() v Setting up prefab instances in Editor performance difference? 1 Answer
How do you make a script that creates a new scene on runtime? 1 Answer