Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How do I create multiple instances of a prefab with their own UI buttons/sliders?

Discussion in 'Scripting' started by gmfbrown, Nov 21, 2020.

  1. gmfbrown

    gmfbrown

    Joined:
    Jan 9, 2019
    Posts:
    25
    I'm trying to create a setup where you have multiple instances of a given prefab, and I want to have a set of controls, containing buttons and sliders, that will control a specific instance. Specifically, it will involve creating X number of spinning levers, which would be accompanied by X number of controls that would set the length, rotation speed, and starting orientation of each lever.

    I know that in Unity there are existing UI game objects for buttons and sliders. The button object has an OnClick event in the inspector that lets you call a specific function on a specific game object when the button is clicked. The slider object has an OnValueChanged event that works in a similar manner.

    However, the only way I know to get the OnClick and OnValueChanged events to actually call a function in a game object when that object is already part of the scene at the start. There also seems to be an option to tell the events to call a function in a prefab, but that seems to create its own instantiation of the prefab.

    As it stands, none of the X spinning levers are in the scene at the very beginning. They're instantiated once the gameplay starts. What do I need to do to make the a button or slider whose OnClick or OnValueChanged events can call a specific instance of the prefab.
     
  2. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    488
    After you instantiate a prefab, depending on if it's a button or a slider, use something like this:

    Button (click for Unity - Scripting API)
    Slider (click for Unity - Scripting API)

    Code (CSharp):
    1. {
    2.     ...
    3.     var createdButton = Instantiate<Button>(...);
    4.     ...
    5.  
    6.     createdButton.onClick.addListener(delegate { OnClickCallback("Test"); });
    7. }
    8.  
    9. private void OnClickCallback(string testArg)
    10. {
    11.     Debug.Log(testArg);
    12. }
    The idea is that you assign callbacks individually to every instantiated button.
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    You can subscribe to events at runtime through script using the AddListener method; e.g. button.OnClick.AddListener(myObjectInstance.MyFunction)

    You can also set up prefabs to already have events pointing to other parts of the same prefab, so if you designed your prefabs in such a way that all the controls for that prefab were actually part of that prefab, you could set them up in the editor through regular means, too.
     
  4. gmfbrown

    gmfbrown

    Joined:
    Jan 9, 2019
    Posts:
    25
    Where would this script go? On the button/slider? Or on the game object the button/slider controls?
     
  5. Dextozz

    Dextozz

    Joined:
    Apr 8, 2018
    Posts:
    488
    Inside a script that instantiates your buttons. Here: