Search Unity

The best way to handle components?

Discussion in 'Scripting' started by jmgek, Aug 13, 2015.

  1. jmgek

    jmgek

    Joined:
    Dec 9, 2012
    Posts:
    177
    Hey guys, I am creating objects and attaching components to them, do you think it would be best to spawn a empty game object and attach children to the object and add components on them(Children)? Or should I just attach components to the objects themselves?
    Like this:

    Empty OBJ
    child AudioSource
    child Light

    Empty OBJ 2
    child Sphere Mesh Render
    child Sphere collider

    Would this act on any performance if I was removing and adding components? Or would the new game objects give me more overhead because of the spawning of new game objects?
     
  2. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    Instantiating new GameObjects and Components will definitely have more overhead than if you just instantiated Components.

    EDIT: But if you really needed the children you could just instantiate 1 prefab with all the components and gameobjects. In fact that might be better than doing it all separately.
     
  3. jmgek

    jmgek

    Joined:
    Dec 9, 2012
    Posts:
    177
    I was thinking more for having the sounds pre loaded on the children and play them as I need them, would you think that may be a little better?
     
  4. SomeGuy22

    SomeGuy22

    Joined:
    Jun 3, 2011
    Posts:
    722
    If you're dealing with AudioSources, it's better to change the clip out and Play() then instantiate a new one, yes.