Search Unity

Weird behavior on scene reset

Discussion in 'Scripting' started by mistergreen2016, Aug 18, 2017.

  1. mistergreen2016

    mistergreen2016

    Joined:
    Dec 4, 2016
    Posts:
    226
    So when my hero dies, the scene is restarted with a simple
    Code (csharp):
    1. SceneManager.LoadScene(sceneName[sceneCount]);
    I initialize some pools in the code below. The only one that gets initialize on the reset is ActiveDustPool. Any idea what's going on?


    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7.  
    8. public class LivingRoomSetup : MonoBehaviour {
    9.  
    10.     public GameObject coinFab;
    11.     public GameObject dustFab;
    12.     public GameObject dustBunnyFab;
    13.     public GameObject toastFab;
    14.     public Text hitCountFab;
    15.  
    16.  
    17.     public GameObject result;
    18.  
    19.  
    20.     // Use this for initialization
    21.     void Start () {
    22.         //hide result
    23.         result.SetActive(false);
    24.  
    25.         //start the pool
    26.         CoinPool.init(coinFab, 5);
    27.  
    28.         ActiveDustPool.init(dustFab, 10);
    29.  
    30.         DustBunnyPool.init(dustBunnyFab, 10);
    31.  
    32.         ToastPool.init(toastFab, 5);
    33.  
    34.         HitCountPool.init(hitCountFab, 5);
    35.  
    36.         Camera.main.GetComponent<SimpleBlit>().FadeToScreen();
    37.  
    38.  
    39.     }
    40.    
    41.  
    42. }
    43.  
    44.  
     
  2. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    So... what is the weird behaviour you're seeing? Are you getting errors or exceptions thrown?

    Hard to say what's going on without knowing what the init function does.
     
  3. mistergreen2016

    mistergreen2016

    Joined:
    Dec 4, 2016
    Posts:
    226
    No errors in restart except when the script tries to get an object pool that doesn't exist.

    This is the typical init function
    Code (csharp):
    1.  
    2. public static int poolNum;
    3.     public static GameObject pooledObj;
    4.     public static List<GameObject> dustPool;
    5.     //public static ActiveDustPool current;
    6.  
    7.  
    8.     // Use this for initialization
    9.     public static void init (GameObject prefab, int initQuantity)
    10.     {
    11.  
    12.         //current = this;
    13.         pooledObj = prefab;
    14.         poolNum = initQuantity;
    15.  
    16.         dustPool = new List<GameObject> ();
    17.  
    18.         //init pool
    19.         for (int i = 0; i < poolNum; i++) {
    20.            
    21.             GameObject obj = (GameObject)GameObject.Instantiate (pooledObj);
    22.             obj.SetActive (false);
    23.             dustPool.Add (obj);
    24.  
    25.         }
    26.         //just set the gameobject to obj.SetActive(false) to send back to pool
    27.  
    28.  
    29.     }
    30.  
    I do notice something. I moved the pool scripts into a folder for organization except I forgot to move ActiveDustPool.cs into the folder. I'll see if reattaching the script will help.

    *******
    I tried reattaching and moving the files out of the folder with no change in result.
     
    Last edited: Aug 18, 2017
  4. mistergreen2016

    mistergreen2016

    Joined:
    Dec 4, 2016
    Posts:
    226
    Oh I found the issue.
    In my other pools I added a null conditional.
    Code (csharp):
    1.  
    2. if(bulletPool == null) {
    3.      //init pool
    4.             for (int i = 0; i < poolNum; i++)
    5.             {
    6.  
    7.                 GameObject obj = (GameObject)GameObject.Instantiate(pooledObj);
    8.                 obj.SetActive(false);
    9.                 bulletPool.Add(obj);
    10.  
    11.             }
    12. }
    13.  
    14.  
    When reset or going to a new scene, The List<GameObject> dustPool isn't remove but the gameObject instances are delete. I guess I need to remember to clean up when going to new scenes. I thought it was automatic.