On Button Click Create a gameobject (ui,no mouse stuff)
I Made a inventory but i want to like when i press a button called "Chair" It will spawn a chair if i click it, and i want a function that i can delete every gameobjects i spawn with the button of my canvas/ui.
This also has happend to my friend but he skipped that part.
Please tell me the script or any tutorials of how to do this, it really makes me mad when i only find a mouse one.
I Want only a function to do: Create gameobject on clicked button ui and able to delete it.
and the script needs something that i can choose what prefav/model i want to choose to put it in
Answer by Juliken · Nov 02, 2018 at 02:57 PM
This looks pretty easy. You can try someting like this:
// Drag and drop your prefab in the Editor
public GameObject ItemToSpawn;
// Store all the Gameobjects spawned
private List<GameObject> ItemsSpawned;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
// Spawn an item
public void SpawnItem()
{
GameObject Item = Instantiate(ItemToSpawn);
ItemsSpawned.Add(Item);
}
// Destroy all items spawned
public void DestroyAllItems()
{
for(int i = 0; i < ItemsSpawned.Count; i++)
{
Destroy(ItemsSpawned[i]);
}
}
In the Unity inspector you put this script on a GameObject. Drag and drop the prefab of the object you want to spawn. Then you assign the functions with your buttons
I hope it will help you :)