- Home /
Create and build a scene from the code?
Hello, does anybody know if there's a way of creating and building (buttons, cubes, etc.) a scene from the code?
The idea is to have a scene that can change depending on the level you are, for example, at level 1 it would have 4 buttons that the player has to press, and at level 2 it would have 8 buttons. Can you change that scene by code? Or should I create a scene fo every level?
Thanks in advance.
Answer by tanoshimi · Oct 28, 2016 at 10:54 AM
Yes, of course. Basically:
int numButtons, levelNum;
if(levelNum == 1) { numButtons = 4;}
else if(levelNum == 2) { numButtons = 8;}
for(int i=0; i<numButtons; i++) { Instantiate(buttonPrefab, Vector3.zero, Quaternion.identity); }
You might want to look through https://docs.unity3d.com/ScriptReference/.
Thanks, but that's for an already created scene. I'm asking to creating a new one. So I have a scene with X buttons, and I want to create a NEW scene with Y buttons.
You cannot create a completely different scene(file) during runtime. You always have to have one existing, but you can populate it procedurally.
Ok, so then the best option would be to have an empty scene (or maybe having the background wich is gonna be the same for all the scenes) and adding the objects by code, right?
You can't create new scenes at runtime. But your project can have a single empty scene whose hierarchy is populated at runtime.
I've just found this:
Shouldn't that work? I'll try it when I get home, but have you used it?
Your answer