#Object Pooling Help
1 messages · Page 1 of 1 (latest)
Can someone help me with my object pool? i watch the tutorial but it didn't works well...
class Spawner : MonoBehaviour
{
PoolingObject objectPooling;
void Start()
{
objectPooling = PoolingObject.Instance;
}
void FixedUpdate()
{
objectPooling.SpawnPool("Square", transform.position);
}
}
i put this script into Spawner GameObject
[System.Serializable]
public class Pool
{
public string objectTag;
public GameObject prefab;
public int size;
}
ah it's okay, thank you for trying to help me i appreciate it.
ok so
I wrote my object pooling system over a year ago
rereading it, but it works pretty solidly
I use animation events to handle my pooled object deactivations, but you can do it however you want
my object pool is working, it's just when it spawn the object it would look like this.
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
im trying to figure it out before asking here. at first i though the error was coming from here
public class ObstacleObject : MonoBehaviour, IPooledObject
{
[HideInInspector]
public int xRange = 3;
private int currentPos = 1;
private List<Vector2> circlePos = new List<Vector2>()
{
new Vector2(-4.3f, 8f),
new Vector2(0.5f, 8f),
new Vector2(4.3f, 8f)
};
void Update()
{
currentPos++;
if (currentPos > 2)
{
currentPos = 0;
}
}
public void OnObjectSpawn()
{
int xPosisition = Random.Range(0 - xRange, xRange + 0);
transform.position = new Vector2(xPosisition, 6f);
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("whut"))
{
transform.position = circlePos[currentPos];
}
}
}
oh thank you i will try it.
that commented portion at the bottom is code you need to put in the prefab being used by the object pool
how you call those methods is up to you
in my case, I set PoolDestroy as public, and call it via an animation event
in the case of my after images, the animation causes them to go from 1 to 0 transparency, and then on the last frame of the animation, an event triggers PoolDestroy
public void LeaveAfterImage(Sprite aiSprite, Vector2 pos, Color color, bool flip, float rotation = 0)
{
GameObject obj = objPool.GetPooledObject();
if (obj == null)
return;
SpriteRenderer aiSpriteRenderer = obj.GetComponent<SpriteRenderer>();
aiSpriteRenderer.sprite = aiSprite;
aiSpriteRenderer.color = color;
aiSpriteRenderer.flipX = flip;
obj.SetActive(true);
obj.transform.position = pos;
obj.transform.rotation = Quaternion.Euler(0,0, rotation);
}```
here is the method that enables the prefabs for after images