Associate UI with gameobject


I currently face the issue of wanting to load specific UI parts (Overlays) when I click on certain game elements, but I couldn’t figure out how to correctly associate the UI prefabs with the object prefabs.

Thanks in advance.

The object prefab can have public variables of the Type of the UI elements. Create that object prefab and then drop the UI prefabs into fields using the inspector.

/* Public Variables */
public GameObject UIOverlay;

/* Private Variables */
Transform canvas;

void Start () {
canvas = GameObject.Find("Canvas").transform;

void CreateOverlay () {
   GameObject overlay = Instantiate (Overlay, position, rotation, canvas) as GameObject; // This sets the overlays parent to the canvas

// Set the overlay layer to the UI layer
overlay.transform.layer = (1 << LayerMask.NameToLayer("UI")); // Possibly remove the .transform, not sure, test it.