Create game object from UI button click even

I know that variations of this question have been asked about 1000 times on this HELP system.

However as a new user, and still new to coding literally none of the answers and or code snippets appear to actually work for my purposes or within my implementation.

This may all be ‘basic’ to the rest of you however it’s just more frustration that make me think that Unity isn’t well ‘unified’.

Here is what I have, a game object as a prefab (yes I figured that part out.

A UI Button

Desired result, when I click the UI button I would want a copy (clone, instantiation) of my prefab
to appear somewhere on my canvas,

I’m not posting any code sample, because as stated nothing I have tried so far works.

What I need, and I expect many other users need this as well.
One clearly working example in C# of a script that when added to a UI button either with
OnClick event
or as
Event Trigger
that produces one new copy of my prefab game object on the screen somewhere each time that button, and ONLY that button is clicked on by the player.

If it helps, the game object prefab has a name of Equip.

Hi @mfarrell80,

[84178-testintanstiation.zip|84178] that shows how to instantiate an object when a button is pressed. The demo will instantiate a prefab (which is just a cube for demo purposes) anywhere within the bounds of the screen.

Below is the script file (which is also in the package) that is used (somewhat commented). If you look at the demo, you will see that the script is attached to the Canvas.

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

// rename this class to suit your needs
public class TestIntanstiation : MonoBehaviour
{
    // the Equip prefab - required for instantiation
    public GameObject equipPrefab;
    
    // list that holds all created objects - deleate all instances if desired
    public List<GameObject> createdObjects = new List<GameObject>();

    private float minX, maxX, minY, maxY;

    void Start()
    {
        // get the screen bounds
        float camDistance = Vector3.Distance(transform.position, Camera.main.transform.position);
        Vector2 bottomCorner = Camera.main.ViewportToWorldPoint(new Vector3(0,0, camDistance));
        Vector2 topCorner = Camera.main.ViewportToWorldPoint(new Vector3(1,1, camDistance));

        minX = bottomCorner.x;
        maxX = topCorner.x;
        minY = bottomCorner.y;
        maxY = topCorner.y;
    }

    public void CreateObject()
    {
        // a prefab is need to perform the instantiation
        if (equipPrefab != null)
        {
            // get a random postion to instantiate the prefab - you can change this to be created at a fied point if desired
            Vector3 position = new Vector3(Random.Range(minX + 0.5f, maxX - 0.5f), Random.Range(minY + 0.5f, maxY - 0.5f), 0);

            // instantiate the object
            GameObject go = (GameObject)Instantiate(equipPrefab, position, Quaternion.identity);
            createdObjects.Add(go);
        }
    }    
}