Search Unity

(Solved) Quiz Tutorial - SimpleObjectPool isn't making a prefab area.

Discussion in 'Scripting' started by Qlet, May 1, 2018.

  1. Qlet

    Qlet

    Joined:
    Apr 7, 2018
    Posts:
    7
    I'm following the Quiz making tutorial and my simpleobjectpool that I copy pasted from the documentation here isn't creating a prefab like you see in the farthest tab in this tutorial video here.
    Help, please.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Do you mean this bit of code?
    Code (csharp):
    1. public class SimpleObjectPool : MonoBehaviour
    2. {
    3.    // the prefab that this object pool returns instances of
    4.     public GameObject prefab;
    That code isn't showing you a variable 'prefab' in the editor to which you can assign the button prefab?

    Please explain if it's that or something else.. Also, be sure to check if you have any compiler errors, as those can sometimes prevent things from working properly in the inspector.
     
  3. Qlet

    Qlet

    Joined:
    Apr 7, 2018
    Posts:
    7
    I'm a total newbie so please be extra patient. Sorry and thanks!
    Here's a visual of what I mean. In his tutorial he has the Prefab space in Inspector while I don't.
    https://imgur.com/a/DKn5676

    I did notice in the code I copy/pasted that one Greater than symbol turned into a gt; and two ampersands turned into && which I corrected but now I am at a loss.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
  5. Qlet

    Qlet

    Joined:
    Apr 7, 2018
    Posts:
    7
    Thank you.

    So I tried compiling again and I have errors CS0305: Using the generic type "Stack<T>" requires 1 type argument and CS0411 The type of arguments for method 'GameObject.AddComponant<T>()' cannot be inferred from the usage. Try specifying the type arguments explicitly.

    Here is my code.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. // A very simple object pooling class
    5. public class SimpleObjectPool : MonoBehaviour
    6. {
    7.     // the prefab that this object pool returns instances of
    8.     public GameObject prefab;
    9.     // collection of currently inactive instances of the prefab
    10.     private Stack inactiveInstances = new Stack();
    11.  
    12.     // Returns an instance of the prefab
    13.     public GameObject GetObject()
    14.     {
    15.         GameObject spawnedGameObject;
    16.  
    17.         // if there is an inactive instance of the prefab ready to return, return that
    18.         if (inactiveInstances.Count > 0)
    19.         {
    20.             // remove the instance from teh collection of inactive instances
    21.             spawnedGameObject = inactiveInstances.Pop();
    22.         }
    23.         // otherwise, create a new instance
    24.         else
    25.         {
    26.             spawnedGameObject = (GameObject)GameObject.Instantiate(prefab);
    27.  
    28.             // add the PooledObject component to the prefab so we know it came from this pool
    29.             PooledObject pooledObject = spawnedGameObject.AddComponent();
    30.             pooledObject.pool = this;
    31.         }
    32.  
    33.         // enable the instance
    34.         spawnedGameObject.SetActive(true);
    35.  
    36.         // return a reference to the instance
    37.         return spawnedGameObject;
    38.     }
    39.  
    40.     // Return an instance of the prefab to the pool
    41.     public void ReturnObject(GameObject toReturn)
    42.     {
    43.         PooledObject pooledObject = toReturn.GetComponent();
    44.  
    45.         // if the instance came from this pool, return it to the pool
    46.         if (pooledObject != null && pooledObject.pool == this)
    47.         {
    48.             // disable the instance
    49.             toReturn.SetActive(false);
    50.  
    51.             // add the instance to the collection of inactive instances
    52.             inactiveInstances.Push(toReturn);
    53.         }
    54.         // otherwise, just destroy it
    55.         else
    56.         {
    57.             Debug.LogWarning(toReturn.name + " was returned to a pool it wasn't spawned from! Destroying.");
    58.             Destroy(toReturn);
    59.         }
    60.     }
    61. }
    62.  
    63. // a component that simply identifies the pool that a GameObject came from
    64. public class PooledObject : MonoBehaviour
    65. {
    66.     public SimpleObjectPool pool;
    67. }
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Add this at the top of your script:
    Code (csharp):
    1. using System.Collections;
    I think that will fix the stack error.
    The AddComponent is not right, either, though.
    Try this, to replace line #29:
    Code (csharp):
    1. PoolObject pooledObject = spawedGameObject.AddComponent<PooledObject>();
     
  7. Qlet

    Qlet

    Joined:
    Apr 7, 2018
    Posts:
    7
    Hello again.
    Sorry it took so long to reply, I was on a holiday.

    Did you mean PoolEDObject pooledObject = spawedGameObject.AddComponent<PooledObject>(); or did you really mean pool instead of pooled?
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ah, yes, that was a typo. ;)
     
  9. Qlet

    Qlet

    Joined:
    Apr 7, 2018
    Posts:
    7
    Ok so I managed to find another code in a template. If anyone's having any trouble, this code seemed to work for me. The main difference I could tell (because I am a total greeny) is that there are a lot more greater than/less than symbols around GameObject.


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections.Generic;
    3.  
    4. // A very simple object pooling class
    5. public class SimpleObjectPool : MonoBehaviour
    6. {
    7.     // the prefab that this object pool returns instances of
    8.     public GameObject prefab;
    9.     // collection of currently inactive instances of the prefab
    10.     private Stack<GameObject> inactiveInstances = new Stack<GameObject>();
    11.  
    12.     // Returns an instance of the prefab
    13.     public GameObject GetObject()
    14.     {
    15.         GameObject spawnedGameObject;
    16.  
    17.         // if there is an inactive instance of the prefab ready to return, return that
    18.         if (inactiveInstances.Count > 0)
    19.         {
    20.             // remove the instance from teh collection of inactive instances
    21.             spawnedGameObject = inactiveInstances.Pop();
    22.         }
    23.         // otherwise, create a new instance
    24.         else
    25.         {
    26.             spawnedGameObject = (GameObject)GameObject.Instantiate(prefab);
    27.  
    28.             // add the PooledObject component to the prefab so we know it came from this pool
    29.             PooledObject pooledObject = spawnedGameObject.AddComponent<PooledObject>();
    30.             pooledObject.pool = this;
    31.         }
    32.  
    33.         // enable the instance
    34.         spawnedGameObject.SetActive(true);
    35.  
    36.         // return a reference to the instance
    37.         return spawnedGameObject;
    38.     }
    39.  
    40.     // Return an instance of the prefab to the pool
    41.     public void ReturnObject(GameObject toReturn)
    42.     {
    43.         PooledObject pooledObject = toReturn.GetComponent<PooledObject>();
    44.  
    45.         // if the instance came from this pool, return it to the pool
    46.         if (pooledObject != null && pooledObject.pool == this)
    47.         {
    48.             // disable the instance
    49.             toReturn.SetActive(false);
    50.  
    51.             // add the instance to the collection of inactive instances
    52.             inactiveInstances.Push(toReturn);
    53.         }
    54.         // otherwise, just destroy it
    55.         else
    56.         {
    57.             Debug.LogWarning(toReturn.name + " was returned to a pool it wasn't spawned from! Destroying.");
    58.             Destroy(toReturn);
    59.         }
    60.     }
    61. }
    62.  
    63. // a component that simply identifies the pool that a GameObject came from
    64. public class PooledObject : MonoBehaviour
    65. {
    66.     public SimpleObjectPool pool;
    67. }