Search Unity

Efficient City Building using Prefabs

Discussion in 'World Building' started by Chronobane, Dec 14, 2018.

  1. Chronobane

    Chronobane

    Joined:
    Dec 24, 2017
    Posts:
    16
    Hello,
    I am quite new to Unity and try to optimize my efficiency using prefabs and nesting those.

    As example: I create a street_lamp prefab where as this contains two lights and a street lamp model.
    This prefab I use in my street_segment prefab which I use multiple in the street prefab.
    Imagine a whole city with dozens of light elements with dozens of streets.

    How would I efficiently build up a system to change the intensity of one light whenever the whole city is build (via script and via editor)?
    I try to be as dynamic as possible regarding changes applied in the end, so it would be nice if you could give me some advice to achieve something like the given example.

    Thanks for your help.
     
  2. Ambrose998800

    Ambrose998800

    Joined:
    Jun 23, 2015
    Posts:
    74
    I'm on the same thing (but a beginner too) - I would say you make a prefab of you streetlamp (Model + Lightsources + Controlscript) and then you place empties on every point at the street prefab (I guess you put them up by prefabs too) you want to assign a street lamp. Then you make a script to that empty (they can share all the same script) that instantiates the prefab of the streetlamp at that position (makes a clone of the streetlamp). Now you can acess every clone of the streetlamp seperatly. Plus, you could set up multiple prefabs of streetlamps (one without problems, one that tilts, on that flickers, one that is dead) and then you can assign them with a random variable automatically - for more variations through the streets.

    Would basicly look like that:
    Code (CSharp):
    1. void PlaceLamp()
    2.     {
    3.         GameObject Newlamp = Instantiate(streetlamp); //streetlamp is the template prefab
    4.  
    5.         Newlamp.transform.SetParent(street.transform); //Assign the lamp to the street in hierarchy
    6.  
    7.         Newlamp.transform.position = transform.position; //Position of the empty
    8.         Newlamp.transform.rotation = transform.rotation; //Rotate the lamp correctly (same as emtpy)
    9.  
    10.         StreetlampList.Add(Newlamp);
    11.     }
    You can then add all lamps into one list to access them from there. If you want you can add the street name and segment name to the name of the lamp: "SampleStreet_Curve2_Streetlamp_3"
    With that you have all lamps stored in a list. You can access their controlscript directly or using the list via index or name filter.
     
    Last edited: Dec 17, 2018
  3. BlankDeedxxAldenHilcrest

    BlankDeedxxAldenHilcrest

    Joined:
    Jul 10, 2018
    Posts:
    292
    I think you want the code on the lamp to access a public boolean for on or off. Then you can change the public at will and the street lights will react accordingly. I think.

    Edit: I thought you wanted on and off.
     
  4. Ambrose998800

    Ambrose998800

    Joined:
    Jun 23, 2015
    Posts:
    74
    I think this isn't a script request, it's about world building techniques (correct me if I'm wrong).

    The trick of changing one certain lamp after everything is set up is in cloning the prefab to wherever you want it and that way, to make independent objects that you can access and change afterwards.
     
    Last edited: Dec 19, 2018
  5. Chronobane

    Chronobane

    Joined:
    Dec 24, 2017
    Posts:
    16
    Yeah @Ambrose998800 this is exactly the question.

    Edit: So to get into detail, how is efficient world building made? Nesting all repetitive elements and save them as prefabs? Or not nesting and copy paste? Or nest and copy paste?
    Are prefabs even considered to be efficient / effective?
     
    Last edited: Dec 18, 2018
  6. Ambrose998800

    Ambrose998800

    Joined:
    Jun 23, 2015
    Posts:
    74
    I'm not a programmer, but I know that problems are coming along with complexity (not just in programms). So, I have for all items in my game just one script that defines everything (except the model). With that I need to make a lot of prefabs, but I have just one script to maintain. In my opinion that makes things more stable cause the script surely is a reference many times to other scripts (inventory, weight calculation, interaction, physics, etc).

    So I would say, make one lamp prefab with one script, by that you can controll every aspect about the lamp you want (the tilting if it is stormy, the insects at night or the powersupply it needs). If you want to access the lamp script via another script, you know exactly how it works (instead of having many different models and scripts with different parameters).

    I don't know how unity handles prefabs exactly. I'm still learning too...
     
  7. NikolasPet

    NikolasPet

    Joined:
    Mar 5, 2017
    Posts:
    4
    Простите у меня свой вопрос:
    В моей модели для игры много повторяющих элементов. Как лучши мне поступить?
    Выставить все элементы в Blender e и после перенести всю модель в Unity.
    Или: Cделать в "Blender e" по одной модели и размножить уже в самом Unity.
     
  8. Tökéletes, egyetlen szavadat sem értem, mert nem angolul írtál.

    (Please use English, my Russian is a little bit rusty, I have to admit I haven't paid too much attention back in the days when in my home country it was mandatory to learn Russian because of the Soviet occupation, although I still know which letter is which, that's something :D)
     
  9. NikolasPet

    NikolasPet

    Joined:
    Mar 5, 2017
    Posts:
    4
    Excuse me my question:
    In my model for the game a lot of repeating elements. What is the best for me?
    All elements are displayed in Blender e and after that the whole model is transferred to Unity.
    Or: Make it in "Blender e".
    P.S. Sorry for the curve English, I use a simple translator ...