Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

"Reset" prefab using Neil's Endless 2D Terrain Generator

Discussion in 'Scripting' started by Badams66, Mar 30, 2014.

  1. Badams66

    Badams66

    Joined:
    Mar 11, 2014
    Posts:
    49
    Using Neil's Endless 2D terrain generator (http://forum.unity3d.com/threads/207138-Endless-2D-Terrain-Generator), I am spawning prefabs which are enemies using the plugins built in prefab generator. The plugin reuses objects rather than instantiating them which is great because I know how expensive instantiate can be, especially since my target platform is Android.

    Neils "PrefabPool" class has a Remove function which disables the prefab, then in the Add function, it either reactivates the object if one exists already, or instantiate a new one.

    The problem I'm running into is that when a player kills an enemy, the terrain generator will reuse that enemy and the end result is a dead enemy is spawned onto the screen, not good.

    I got around this temporarily by Destroying the object in the Remove function. As mentioned earlier though, this causes the plugin to instantiate a new object, and I can tell the performance decrease when playing on a hardware device, also not good.

    I have initialized all my variables in the Start() function on my enemy script, but it turns out Start() is only called when the object is created and not when it is set active, so that is out.

    I have no problems writing a "reset" function and initialize the variables in there to make the enemy "alive" but I cant figure out how to call that function from within the plugins PrefabPool class.

    This is what the remove function of the PrefabPool class looks like:

    Code (csharp):
    1.         public void Remove(GameObject prefab)
    2.         {
    3.             var prefabToRemove = Prefabs.Where(p => p.Prefab == prefab).FirstOrDefault();        
    4.             prefabToRemove.IsSpawned = false;
    5.             prefabToRemove.Prefab.SetActive(false);
    6.         }
    Ideally id like to do something like this once the object is disabled in the function above

    Code (csharp):
    1. prefabToRemove.Prefab.gameObject.GetComponent<EnemyScript>().reset();
    But I am not able to call a function like that, I believe due to the fact that the class is not derived from MonoBehaviour (I could be wrong on this though).

    Just looking for a suggestion or a push in the right direction :)
     
    Last edited: Apr 8, 2014
  2. casperjeff

    casperjeff

    Joined:
    Apr 13, 2013
    Posts:
    333
    I ran into same thing....
    the key is to use Unity's 'OnEnable' function on your gameobjects
    This function will be called when his pool re-uses those objects.

    You can use OnDisable() as well

    Hope this helps!
     
  3. Badams66

    Badams66

    Joined:
    Mar 11, 2014
    Posts:
    49
    Ahhh, that is perfect!
    Just tested and works like a charm!
    Knew I could count on you casperjeff! lol

     
    Last edited: Apr 8, 2014
  4. casperjeff

    casperjeff

    Joined:
    Apr 13, 2013
    Posts:
    333
    Now - one thing to note. I have objects that I don't want to start 'activity' (falling, targeting, etc) until they are in range of the player.
    So I have to remember in my OnDisable and OnEnable functions to ensure that that doesn't take place immediately when the pool re-uses them.
    In my case, it was flipping certain flags and turning off gravity and such.