Can't see clones instantiated on button press unless I make them children of the canvas

I am rather new to Unity, but I’ve read some similar questions and altering the Z axis does not seem to make a difference.

I start with the green diamond button and a rover image in the top right canvas to show that it can be seen.
Then I click the button and the clones are instantiated, but I can’t see them.


Then if I can drag a clone in the hierarchy so that it is a child of the canvas then I can see it (but only in the bottom left corner)

Is there a way that the clones can be a child of the canvas immediately upon being instantiated?
Also I know that this has 4 clones being made evenly spaced apart, it was being used for cloning sprites and worked.

Thank you!
and sorry that this script looks so weird, I am not sure the best way to format this text here.
.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class Maketherover : MonoBehaviour
{
public UnityEvent buttonClick;
public Transform Rover;
void Awake()
{
if (buttonClick == null) { buttonClick = new UnityEvent(); }
}

void OnMouseDown()
{
    for (int i = 0; i < 4; i++)
    {
        Instantiate(Rover, new Vector3(i * 3, -2, -2), Quaternion.identity);
    }
}

}

There are 2 paths you can take to this:


Path 1 - use gameObject.transform.setParent().

  • Instantiate() returns the newly
    cloned GameObject which you can then
    use to set the new parent after
    creation.

Path 2 - Object pooling

  • If you create/destroy a lot of
    GameObjects throughout your game
    cycle, one optimization path is to
    create all objects up front that will
    be re-used throughout the
    application.
  • This means you will have an array, or other object container, of all your GameObjects up front with
    visibility set to false until needed
    in the scene.

Hope this helps!!

Thanks a bunch for your quick reply @djenningsais !
The gameObject.transform.setParent() is what makes the most sense to me, but someone working on the project apparently changed something and now it shows up without me having added anything to the script.
Take care :slight_smile: