Search Unity

gameobject pool for gameobject with child

Discussion in 'Scripting' started by LaurensiusTony, Jul 4, 2014.

  1. LaurensiusTony

    LaurensiusTony

    Joined:
    May 22, 2013
    Posts:
    78
    Hi, i tried some gameobject pool code from somewhere in the internet, i forgot where i get that but it work for a single gameobject, when i trying to pool gameobject that have child, its not working, i think it's because my gameobject with child is like this:

    emptygameobject
    -cube1 (have health script, move, collider)
    -cube2 (have health script, move, collider)
    -cube3 (have health script, move, collider)

    so i use emptygameobject to make it instantiate 3 at the same time and move together and when the cube die the emptygameobject is still active so i thinking is there a way to solve this problem?

    and there is some case have gameobject like this

    emptygameobject
    -body (have health script, move, collider) and when this body destroyed/ hp < 0 it will setactive(false) the parent
    using this transform.parent.gameObject.SetActive(false);
    -turret (have health script, move, collider)

    but when this gameobject active again it just active the emptygameobject and the body not with the turret

    here is the code for pooling (i have change a litlle because the code i get is already old and still using active and SetActiveRecursively and i'm getting warning that this code are deprecated)

    Code (CSharp):
    1. GameObject[] enemies = null;
    2. public int enemyNum = 0;
    3.  
    4. //start
    5. for (int i = 0; i < enemyNum; i++)
    6. {
    7.     enemies[i] = Instantiate(obj[Random.Range(0, obj.GetLength(0))],
    8.         transform.position, Quaternion.identity) as GameObject;
    9.     enemies[i].SetActive(false);
    10. }
    11. spawn();
    12.  
    13.  
    14. //spawn method
    15. void spawn()
    16. {
    17.     for (int i = 0; i < enemyNum; i++)
    18.     {
    19.         if (enemies[i].activeSelf == false)
    20.         {
    21.             enemies[i].SetActive(true);
    22.             enemies[i].transform.position = transform.position;
    23.             isSpawn = true;
    24.             Invoke("Spawn", Random.Range(spawnMin, spawnMax));
    25.             return;
    26.         }
    27.     }
    28. }
     
  2. christides11

    christides11

    Joined:
    May 19, 2012
    Posts:
    667
    I have a pooling solution, works pretty well. I dunno what's wrong with your script, but I've used mine with objects that have children. Make sure to place the script on a empty gameobject (or another object that also travels inbetween scenes).
     

    Attached Files:

  3. LaurensiusTony

    LaurensiusTony

    Joined:
    May 22, 2013
    Posts:
    78
    thank for sharing you code is more flexible than mine(since it have growing capabilities) but my problem still exist, how you destroy/setactive(false) to gameobject that have child and that child is the one that have health and if the health less than 0 then it will setactive false but the parent object remain true so in you code it will keep instantiating new gameobject and leaving the old gameobject parent stay active but the child not active.

    maybe my way is wrong in preparing my prefabs.....
    i just want to have a group of enemies that come out in the same movement like 3 cube that move together since my game is much like tower defence concept

    oh and a little question about performance in mobile, since you code instantiate gameobject on the fly how it will be compared to the one like mine that instantiate everything in the start? is it more efficient in memory?
     
  4. christides11

    christides11

    Joined:
    May 19, 2012
    Posts:
    667
    No clue on doing that, unless the parent has a script that checks the health of the child and set's it false (or the child does it itself on enable).

    I've used it for a mobile game, but yea you may want to instantiate a few of the needed objects on start for better performance.
     
  5. LaurensiusTony

    LaurensiusTony

    Joined:
    May 22, 2013
    Posts:
    78
    after doing a little more research i found that setactive(true) only making my parent object become true and leave other child object remain not active.... i'm getting very confuse now, since in documentation it will be affect all including child object
     
unityunity