Search Unity

Combine children of runtime instantiated objects

Discussion in 'Scripting' started by gtjuggler, Mar 2, 2009.

  1. gtjuggler

    gtjuggler

    Joined:
    Nov 13, 2008
    Posts:
    238
    Hey all,

    I know there is a combine children script that you can apply to a parent and it will combine all children into one object.

    Can I apply a similar procedure to spheres that I instantiate at runtime?

    In my game, I create spheres that act as nodes on a randomly generated map (think turning points in a maze). I make 10 of them at runtime in specific locations and so I'm using up 10 draw calls. Can I instantiate them then write some code that combines them all into one object? All they do is sit there and animate their color and they all animate the same way at the same time, so I would love to be able to combine them.

    How would I accomplish this?
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Yes, make an empty game object in your project, attach the CombineChildren script to it, and deactivate it. In your script, have the spheres get parented to that empty object when they are instantiated, and then activate the empty object (which causes the combine children script to run). Presto-combino!

    --Eric
     
  3. gtjuggler

    gtjuggler

    Joined:
    Nov 13, 2008
    Posts:
    238
    Alright, so any reason why it wouldn't actually combine the objects? No error thrown, and it childs the spheres under my gameobject, but they're still separate objects, and still drawing 10 draw calls.

    I know this would happen if they were all using different textures but it looks like everything is the same because I create them all in the same way.

    Here's how i create the spheres:

    dot = GameObject.CreatePrimitive(PrimitiveType.Sphere);
    dot.transform.parent = sphereContainer.transform;
    dot.renderer.material = ballmat;
    dot.transform.position = game.store.pos(card_node, 0);
    dot.transform.localScale = new Vector3(.3,.3,.3);
    dot.AddComponent("pulse");

    The pulse part is a script which modifies the spheres' materials to pulsate their color from one color to another, but they all do the same thing so it shouldn't be a problem. I disabled the pulse script and the adding of it as a component to the spheres, and the combine children script still does not combine.

    This is on IPHONE btw.

    Ideas?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You definitely wouldn't want the pulse script to be on each of the children, but only on the combined parent. You should also not add the material to the children, but add it to the parent instead. Make sure the parent is not activated when the scene starts, and that you activate it after the children are all added, or else the combining script won't run.

    --Eric
     
  5. Fubiou

    Fubiou

    Joined:
    May 8, 2010
    Posts:
    43
    Your methood indeed works but for exemple, I want to instantiate blood marks and I want them combined, so every mark created would be combined but in your way it just works the first time you use it, do you have a clue about how to make it work in runtime and many different times?
     
  6. _md_

    _md_

    Joined:
    Dec 19, 2012
    Posts:
    1
    I think the problem comes from dot.renderer.material :

    If you do this, an instance of the material is created, and you get a copy of the material, not the material from the resources of your project.
    1 material = 1 draw call
    The solution should be : dot.renderer.sharedMaterial = ballmat;

    This is what I understand about Unity, if I'm wrong, please explain