Colliding prefab GameObject that is attach to canvas with another gameobject not attach canvas

  • I have a panel that is child of canvas

  • use the panel to instantiate some gameobject (cards)

  • these are visible on canvas but not on main camera

  • Another gameobject (enemy) is not attach to canvas but is instantiated in their given position

  • this is not visible on canvas but is visible on main camera

  • I am trying to collide both gameobjects (cards and enemy) with OnTriggerEnter2D but it does not collide

  • the card has a rigidbody2d and a box collider with isTrigger checked

  • the enemy just has a box collider (isTrigger unchecked)

  • the code is on the card:

private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == “Enemy”)
{
print(“collided”);
}

}
  • is there anyway to collide both gameobjects?

My question is why keep objects in the canvas… The canvas is for the UI.
Use a Render Texture to display game world objects on screen.

  • Assets>Create>Render Texture
  • Create a New Camera and place it in a
    good position for visualize the
    objects you need and assign the
    Render Texture to Target Texture
  • Create a RawImage in the Canvas and
    assign again the Render Texture to
    the Texture 2D
  • In the Canvas check that Main Camera
    is assign to the Render Camera
  • Set Clear Flags of the Cameras to
    look better without overlaps