Search Unity

Question How to add a button with another button?

Discussion in 'UGUI & TextMesh Pro' started by HolyWaddaWadda, Oct 19, 2022.

  1. HolyWaddaWadda

    HolyWaddaWadda

    Joined:
    Aug 17, 2021
    Posts:
    10
    What the question is. I'm asking how can i make it so that when you press a button in a UI it makes another button.

    So far i have this code which i modified from a script that can add physical game objects as buttons, but i don't know how to make a completely new button from another button.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class KayakManager : MonoBehaviour
    7. {
    8.     public static KayakManager Instance;
    9.     public List<Item> Items = new List<Item>();
    10.  
    11.     public Transform ItemContent;
    12.     public GameObject FoodItem;
    13.     public GameObject Kayak;
    14.  
    15.     public KayakItemController[] InventoryItems;
    16.  
    17.     private void Awake()
    18.     {
    19.         Instance = this;
    20.     }
    21.  
    22.     public void Add(Item item)
    23.     {
    24.         Items.Add(item);
    25.     }
    26.  
    27.     public void ListItems()
    28.     {
    29.         // Prevents the items from multiplying every time the player opens the inventory
    30.         foreach (Transform item in ItemContent)
    31.         {
    32.             Destroy(item.gameObject);
    33.         }
    34.  
    35.         foreach (var item in Items)
    36.         {
    37.             Button but =
    38.         }
    39.  
    40.         SetInventoryItems();
    41.     }
    42.  
    43.     public void SetInventoryItems()
    44.     {
    45.         InventoryItems = ItemContent.GetComponentsInChildren<KayakItemController>();
    46.  
    47.         for (int i = 0; i < Items.Count; i++)
    48.         {
    49.             InventoryItems[i].AddItem(Items[i]);
    50.         }
    51.     }
    52. }
    53.  
     
  2. karliss_coldwild

    karliss_coldwild

    Joined:
    Oct 1, 2020
    Posts:
    602
    Just instantiate them from prefab just like you would do with with any other game object. Don't forget to set the parent, as UI objects will not work properly if they are not parented somewhere under a canvas. Preferably do the parenting when instantiating by using appropriate Instantiate overload instead of assigning parent afterwards, otherwise there is a chance of messing up scale.

    You might also want to read this https://docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/HOWTO-UICreateFromScripting.html