Skip to main content
deleted 3 characters in body
Source Link
Philipp
  • 121.5k
  • 28
  • 261
  • 342

A collision requires that at least one of the objects involved in the collision has a Rigidbody component and the other object is either marked as Static or also has a Rigidbody component. For more information, check the manual article on colliders. It appears that your pipe game object is neither static (and it shouldn't be, because it moves) nor has a rigidbody (but it probably should). I can't see your destroyer, but I would assume that it does not have a rigidbody either.

However, in this scenario you likely don't want to use collisions, you want to use triggers. Mark the destroyer as static, and give it a 2D collider with the "IsTrigger" property activated. This turns it into a "Static trigger collider".

It will now be triggered by anything with a Rigidbody2D attached to it. So attach the Rigidbody2D component to your pipes (not the regular Rigidbody - that one only interacts with regular 3d colliders, not with 2D colliders). You probably want that Rigidbody2D to be marked as the body type "Kinetic", because you want to control its movement directly and not leave it to the physics engine.

When you have trigger events instead of collision events, then you don't need to implement OnCollisionEnter2D, you need to implement OnTriggerEnter2D. Note that the parameter of OnTriggerEnter2D is not a Collision2D object, it's the Collider2D component of the other gameObject. When you declare it as OnTriggerEnter2D(Collision2D other) then it won't get recognized by the engine and won't anything. You have to do it like this:

void OnTriggerEnter2D(Collider2D otherCollider)
{
    if (otherCollider.CompareTag("Destroy")) {
        Debug.Log("hit");  
        Destroy(otherCollider.gameObject); 
        SpawnPipe();         
    }
}

A collision requires that at least one of the objects involved in the collision has a Rigidbody component and the other object is either marked as Static or also has a Rigidbody component. For more information, check the manual article on colliders. It appears that your pipe game object is neither static (and it shouldn't be, because it moves) nor has a rigidbody (but it probably should). I can't see your destroyer, but I would assume that it does not have a rigidbody either.

However, in this scenario you likely don't want to use collisions, you want to use triggers. Mark the destroyer as static, and give it a 2D collider with the "IsTrigger" property activated. This turns it into a "Static trigger collider".

It will now be triggered by anything with a Rigidbody2D attached to it. So attach the Rigidbody2D component to your pipes (not the regular Rigidbody - that one only interacts with regular 3d colliders, not with 2D colliders). You probably want that Rigidbody2D to be marked as the body type "Kinetic", because you want to control its movement directly and not leave it to the physics engine.

When you have trigger events instead of collision events, then you don't need to implement OnCollisionEnter2D, you need to implement OnTriggerEnter2D. Note that the parameter of OnTriggerEnter2D is not a Collision2D object, it's the Collider2D component of the other gameObject. When you declare it as OnTriggerEnter2D(Collision2D other) then it won't get recognized by the engine and won't anything. You have to do it like this:

void OnTriggerEnter2D(Collider2D otherCollider)
{
    if (otherCollider.CompareTag("Destroy")) {
        Debug.Log("hit");  
        Destroy(otherCollider.gameObject);          
    }
}

A collision requires that at least one of the objects involved in the collision has a Rigidbody component and the other object is either marked as Static or also has a Rigidbody component. For more information, check the manual article on colliders. It appears that your pipe game object is neither static (and it shouldn't be, because it moves) nor has a rigidbody (but it probably should). I can't see your destroyer, but I would assume that it does not have a rigidbody either.

However, in this scenario you likely don't want to use collisions, you want to use triggers. Mark the destroyer as static, and give it a 2D collider with the "IsTrigger" property activated. This turns it into a "Static trigger collider".

It will now be triggered by anything with a Rigidbody2D attached to it. So attach the Rigidbody2D component to your pipes (not the regular Rigidbody - that one only interacts with regular 3d colliders, not with 2D colliders). You probably want that Rigidbody2D to be marked as the body type "Kinetic", because you want to control its movement directly and not leave it to the physics engine.

When you have trigger events instead of collision events, then you don't need to implement OnCollisionEnter2D, you need to implement OnTriggerEnter2D. Note that the parameter of OnTriggerEnter2D is not a Collision2D object, it's the Collider2D component of the other gameObject. When you declare it as OnTriggerEnter2D(Collision2D other) then it won't get recognized by the engine and won't anything. You have to do it like this:

void OnTriggerEnter2D(Collider2D otherCollider)
{
    if (otherCollider.CompareTag("Destroy")) {
        Debug.Log("hit");  
        Destroy(otherCollider.gameObject); 
        SpawnPipe();         
    }
}
deleted 3 characters in body
Source Link
Philipp
  • 121.5k
  • 28
  • 261
  • 342

A collision requires that at least one of the objects involved in the collision has a Rigidbody component and the other object is either marked as Static or also has a Rigidbody component. For more information, check the manual article on colliders. It appears that your pipe game object is neither static (and it shouldn't be, because it moves) nor has a rigidbody (but it probably should). I can't see your destroyer, but I would assume that it does not have a rigidbody either.

However, in this scenario you likely don't want to use collisions, you want to use triggers. Mark the destroyer as static, and give it a 2D collider with the "IsTrigger" property activated. This turns it into a "Static trigger collider".

It will now be triggered by anything with a Rigidbody2D attached to it. So attach the Rigidbody2D component to your pipes (not the regular Rigidbody - that one only interacts with regular 3d colliders, not with 2D colliders). You probably want that Rigidbody2D to be marked as the body type "Kinetic", because you want to control its movement directly and not leave it to the physics engine.

When you have trigger events instead of collision events, then you don't need to implement OnCollisionEnter2D, you need to implement OnTriggerEnter2D. Note that the parameter of OnTriggerEnter2D is not a Collision2D structureobject, it's the Collider2D component of the other gameObject. When you declare it as OnTriggerEnter2D(Collision2D other) then it won't get recognized by the engine and won't anything. You have to do it like this:

void OnTriggerEnter2D(Collider2D otherotherCollider)
{
    if (colotherCollider.CompareTag("Destroy")) {
        Debug.Log("hit");  
        Destroy(otherCollider.gameObject);          
    }
}

A collision requires that at least one of the objects involved in the collision has a Rigidbody component and the other object is either marked as Static or also has a Rigidbody component. For more information, check the manual article on colliders. It appears that your pipe game object is neither static (and it shouldn't be, because it moves) nor has a rigidbody (but it probably should). I can't see your destroyer, but I would assume that it does not have a rigidbody either.

However, in this scenario you likely don't want to use collisions, you want to use triggers. Mark the destroyer as static, and give it a 2D collider with the "IsTrigger" property activated. This turns it into a "Static trigger collider".

It will now be triggered by anything with a Rigidbody2D attached to it. So attach the Rigidbody2D component to your pipes (not the regular Rigidbody - that one only interacts with regular 3d colliders, not with 2D colliders). You probably want that Rigidbody2D to be marked as the body type "Kinetic", because you want to control its movement directly and not leave it to the physics engine.

When you have trigger events instead of collision events, then you don't need to implement OnCollisionEnter2D, you need to implement OnTriggerEnter2D. Note that the parameter of OnTriggerEnter2D is not a Collision2D structure, it's the Collider2D component of the other gameObject. When you declare it as OnTriggerEnter2D(Collision2D other) then it won't get recognized by the engine and won't anything.

void OnTriggerEnter2D(Collider2D other)
{
    if (col.CompareTag("Destroy")) {
        Debug.Log("hit");            
    }
}

A collision requires that at least one of the objects involved in the collision has a Rigidbody component and the other object is either marked as Static or also has a Rigidbody component. For more information, check the manual article on colliders. It appears that your pipe game object is neither static (and it shouldn't be, because it moves) nor has a rigidbody (but it probably should). I can't see your destroyer, but I would assume that it does not have a rigidbody either.

However, in this scenario you likely don't want to use collisions, you want to use triggers. Mark the destroyer as static, and give it a 2D collider with the "IsTrigger" property activated. This turns it into a "Static trigger collider".

It will now be triggered by anything with a Rigidbody2D attached to it. So attach the Rigidbody2D component to your pipes (not the regular Rigidbody - that one only interacts with regular 3d colliders, not with 2D colliders). You probably want that Rigidbody2D to be marked as the body type "Kinetic", because you want to control its movement directly and not leave it to the physics engine.

When you have trigger events instead of collision events, then you don't need to implement OnCollisionEnter2D, you need to implement OnTriggerEnter2D. Note that the parameter of OnTriggerEnter2D is not a Collision2D object, it's the Collider2D component of the other gameObject. When you declare it as OnTriggerEnter2D(Collision2D other) then it won't get recognized by the engine and won't anything. You have to do it like this:

void OnTriggerEnter2D(Collider2D otherCollider)
{
    if (otherCollider.CompareTag("Destroy")) {
        Debug.Log("hit");  
        Destroy(otherCollider.gameObject);          
    }
}
Source Link
Philipp
  • 121.5k
  • 28
  • 261
  • 342

A collision requires that at least one of the objects involved in the collision has a Rigidbody component and the other object is either marked as Static or also has a Rigidbody component. For more information, check the manual article on colliders. It appears that your pipe game object is neither static (and it shouldn't be, because it moves) nor has a rigidbody (but it probably should). I can't see your destroyer, but I would assume that it does not have a rigidbody either.

However, in this scenario you likely don't want to use collisions, you want to use triggers. Mark the destroyer as static, and give it a 2D collider with the "IsTrigger" property activated. This turns it into a "Static trigger collider".

It will now be triggered by anything with a Rigidbody2D attached to it. So attach the Rigidbody2D component to your pipes (not the regular Rigidbody - that one only interacts with regular 3d colliders, not with 2D colliders). You probably want that Rigidbody2D to be marked as the body type "Kinetic", because you want to control its movement directly and not leave it to the physics engine.

When you have trigger events instead of collision events, then you don't need to implement OnCollisionEnter2D, you need to implement OnTriggerEnter2D. Note that the parameter of OnTriggerEnter2D is not a Collision2D structure, it's the Collider2D component of the other gameObject. When you declare it as OnTriggerEnter2D(Collision2D other) then it won't get recognized by the engine and won't anything.

void OnTriggerEnter2D(Collider2D other)
{
    if (col.CompareTag("Destroy")) {
        Debug.Log("hit");            
    }
}