Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Object Pooling with Particles

Discussion in 'Scripting' started by JungaBoon, Nov 14, 2020.

  1. JungaBoon

    JungaBoon

    Joined:
    Aug 4, 2020
    Posts:
    17
    So I'm trying to use object pools for the first time. I have two pools (Object A = projectile, Object B = trails).

    I'm trying to spawn A and B simultaneously and parent B to A. I've gotten A to spawn just fine, but for some reason whenever I try to spawn B, I keep getting this error:
    upload_2020-11-13_23-39-15.png

    Here is the code snippet:
    Code (CSharp):
    1.     ObjectPooler2 objectPooler;
    2.     GameObject trailParticle;
    3.  
    4.   public void OnObjectSpawn()
    5.     {
    6.         spawnLocation = transform.position;
    7.  
    8.         trailParticle = objectPooler.SpawnFromPool("Player_BasicParticle", transform.position, Quaternion.identity);
    9.         trailParticle.transform.SetParent(gameObject.transform);
    10.     }
    I want to parent B to A and then somehow stop the trails from playing once it collides with something. My previous method used Instantiate, and then unparent, and then letting the trails play themselves out. But I was hoping that object pooling would improve performance. Thoughts?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,722
    Most likely either
    objectPooler
    is null or
    objectPooler.SpawnFromPool
    is returning null (I assume line 8 or 9 in your snippet is line 34 in your script?)

    How are you assigning the value of
    objectPooler
    ?
    Did you write the object pooler script yourself?
     
  3. JungaBoon

    JungaBoon

    Joined:
    Aug 4, 2020
    Posts:
    17
    Oh! So it turned out I forgot to add this in the OnObjectSpawn: objectPooler = ObjectPooler2.Instance;

    That fixed it. Thanks!

    The Object Pool script was from this Brackeys video
     
  4. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    An
    ObjectPooler2
    class name implies the existence of another
    ObjectPooler
    class.
    If you need multiple object pools, the singleton pattern is not one you'd want to use here.

    Rather than needing to make
    ObjectPooler3/4/5/6...
    classes, it would be better to remove "Instance" and just reference an ObjectPooler as any other MonoBehavior component or C# object instance.