- Home /
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 o 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);
}
}
Answer by Xarbrough · Jul 11, 2015 at 06:42 PM
Creating UI elements from scripting
See this tutorial on how to correctly create UI elements from code.
In short: It needs to be parented to a Transform below a Canvas.
Your answer
Follow this Question
Related Questions
Scaling resolution for Mobile devices using UI elements 1 Answer
2D - Rotate Arm around Joint! 0 Answers
Everytime I Instantiate, object becomes smaller. 2 Answers
Spawn: Instantiate GameObject 1 Answer