# how to instantiate UI image in unity2D

I'm making 2d racing game .... and I'm using UI image in the canvas background (road) .my car and enemy cars and I have tried to instantiate enemy car with this script by attaching this script to empty gameObject then attaching the enemy prefab to the enemyCar image that in the script...but the strange thing is :the enemyCar is instantiated in the hierarchy but I can't see it in the game What is the wrong ? is the wrong related to the child and parent or what ? and how can I make that UI image a child of canvas in the script ?

using UnityEngine;
using UnityEngine.UI;
public class Generate : MonoBehaviour
{
public Image enemyCar;

// Use this for initialization
void Start()
{
InvokeRepeating("CreateEnemy", 1f, 1.5f);
}

void CreateEnemy()
{
Instantiate(enemyCar);
}
}


Prefab the canvas and image then add this code to Instantiate it within the canvas.

public GameObject canvas;
public GameObject enemyCar;

void CreateEnemy(){
var createImage = Instantiate(enemyCar) as GameObject;
createImage.transform.SetParent(canvas.transform, false);
}


Almost exactly the same answer I posted yesterday on how to Instantiate A Button The main thing is to instantiate within the canvas.

edit:

If you are also Instantiating the canvas

    void CreateEnemy() {
GameObject newCanvas = Instantiate(canvas) as GameObject;
GameObject createImage = Instantiate(enemyCar) as GameObject;
createImage .transform.SetParent(newCanvas.transform, false);
}

• really thanks it works ! but there is a small problem : I made the same thing with enemy car and the road and make it moving downwards but the instantiated road clone become above my car an I can't see my car even the instantiated enemyCar .. the road become above them and and I can't see both my car end enemy car .. I see only the instantiated road clone how to make myCar and the instantiated enemyCar above my the nstantiated road clone ? Jul 11 '15 at 19:17
• but how can I do that with my car(thePlayer) to make it alawys at the top .. as I only instantiate the enemyCar and the road Jul 12 '15 at 0:06
• edited the answer to include the Instantiate canvas. Jul 12 '15 at 0:11