- Home /
Object references get mixed after delete
This one might be hard to explain.
I'm making a game like Dr. Mario where objects fall from the top of the screen and stack. When there are three objects stacked they are supposed to delete. My code works fine on the first delete but once they stack the second time they do not delete. I could be way off but based on debug statements I placed in my code I think that the collision.gameobject and the gameobject are getting mixed up and therefore is never getting deleted twice. Let me explain:
public class PillBehavior : MonoBehaviour {
GameObject test; // the current pill beign dropped
public GameObject next; // makeshift pointer holding the pill that this object is colliding with
public GameObject prev; // more makeshift pointers
void OnCollisionEnter(Collision collision){
collision.gameObject.GetComponent<PillBehavior> ().next = test;
test.GetComponent<PillBehavior> ().prev = collision.gameObject;
//if there is an object on either side of the collided with object
if (collision.gameObject.GetComponent<PillBehavior>().prev != null && collision.gameObject.GetComponent<PillBehavior>().next != null )
{
if(collision.gameObject.GetComponent<PillBehavior>().prev != collision.gameObject.GetComponent<PillBehavior>().next)
{
Destroy (test.gameObject);
Destroy (collision.gameObject.GetComponent<PillBehavior> ().prev);
Destroy (collision.gameObject);
} }
On the first time through this will delete the 3 objects that are stacked on top of each other. But on the second time through when there is a collision it just does not even recognize the collision.gameObject. All of the debug statements for the collision.gameObject and for the falling test come back as test.
I hope that makes sense
Thanks for any help!
Your answer
