I understand most behavior scripts in Unity are properly attached to the object they deal with. But how would I set up a script and Start routine which attempts to dynamically build a scene? E.g. to, right at the beginning, iterate over an array to build 10 cubes via, I think, GameObject.CreatePrimitive(). Thanks!
5 Answers
5You can attach to Main Camera
Every Scene has a camera object, so you could attach your scene building script to this camera. An other option is to create an empty object and rename it “SceneBuilder” or so for this porpuses.
You could keep it on an empty object called Scene Builder or anything. It doesn’t matter. The Start() function is executed at the start/load of the scene, no matter what object it is on.
any script in a scene can do this. There is no notion of “main” script.
Just put a game object in your scene (or use one existant as the main camera) and add a script that builds the scene during the call to Start().
unity doesnt require that you use a main function, and this gives you some flexibility in how you manage your game, you can trigger the events based on eachother very similarly to how you are going to be doing them in your main class, however even for myself i like a central location to manage from.
your probably best off making a new gameObject and using it for your “main” script, and have everything else referred to, or called in
You can create an empty game object and attach your script to it. In this script you can put your cube creation inside Awake() method.
– HarshadK