Question by
SageGlaze · Apr 04, 2019 at 06:28 AM ·
c#collisionobject pool
Object pooling tries to deactivate object twice?
I'm not sure if the fact that I'm using Pooly makes any difference to this issue but whenever I fire one of my pooled projectiles at the point where two objects meet, like the crevice between a wall and the floor, I get a Debug.Log message that says "[Pooly] Tried to despawn a clone of 'prefab' but it was already despawned!"
It appears that OnCollisionEnter is being called for both the wall and the floor, while I'd like to think this isn't an immediately damaging behavior, I'm worried this could cause increased GC which is of course something I'm trying to avoid. How can I prioritize one collision over the other? Here's my code if it helps:
public float speed;
public Transform projHitClone;
private Attack_Master attackMaster;
private Rigidbody rb;
void OnEnable()
{
attackMaster = GetComponent<Attack_Master>();
rb = gameObject.GetComponent<Rigidbody>();
}
void Update()
{
if(speed != 0)
{
transform.position += transform.forward * (speed * Time.deltaTime);
}
}
void OnCollisionEnter(Collision col)
{
Debug.Log("Collision Enter: " + col.gameObject.name);
if(col.transform.CompareTag("Enemy"))
{
attackMaster.CallEventHitEnemy(col.transform);
}
rb.angularVelocity = Vector3.zero;
rb.velocity = Vector3.zero;
ContactPoint contact = col.GetContact(0);
Quaternion rot = Quaternion.FromToRotation(Vector3.up, contact.normal);
Vector3 pos = contact.point;
Pooly.Spawn(projHitClone, pos, rot);
speed = 0;
}
void OnCollisionExit(Collision col)
{
Debug.Log("Collision Exit: " + col.gameObject.name);
}
Comment