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

Question Attaching a UI button to an instantiated prefab

Discussion in 'Scripting' started by warrenbrandt, Apr 13, 2023.

  1. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    Hi guys,

    I have a prefab that i want the player to click on, but it will be instantiated in different positions. How can I get the button to follow where the object is so its over the object when its instantiated at different positions?
    Right now I just have a button on the canvas in the hierarchy which works but its not following the object.
     
  2. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    316
    Is the button part of the prefab? Or is the prefab a normal gameobject and you want the button in the UI to be exactly over that? Over the last spawned object or over every object?
    Can you describe what you want to do a bit more in context because it sounds to me that you don't need a button but just want to be able to click on the object which can be done by implementing the IPointerClickHandler interface (https://docs.unity3d.com/2018.1/Documentation/ScriptReference/EventSystems.IPointerClickHandler.html)

    Otherwise, it should be possible to convert the world space coordinates of the object to screen space UI coordinates and move the Button there but with multiple objects you would need multiple buttons and maybe different aspect ratios of the screen will make it difficult, I haven't tried mixing UI and 3D space too much.

    Can you make a screenshot of your scene maybe with spawned objects?
     
  3. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    Oh, hey Olipool!

    Is the button part of the prefab? not at the moment its just on the Canvas in the hierarchy, is it better if its part of the prefab? I want to instantiate the prefab at random locations, and i need the button to be over the instantiated prefab, so you can click on it.

    I'm making a simple game for my 4 year old nephew. When the frog appears and he clicks on it the frog goes "Ribbet"
    which is happening now. but the button isnt following the frog when i move it.
     
  4. Olipool

    Olipool

    Joined:
    Feb 8, 2015
    Posts:
    316
    Oh, nice goal :)
    As I said I don't have much experience aligning UI buttons to world space objects so maybe someone else can answer that more competently.
    For what you are trying to do, you could move the frogs into the UI so they can act like buttons. A frog could then be a button without text but with an image of the frog attached. By that you can even get interaction feedback for free. If you set the button transition effect to sprite swap you can also have the frog open its mouth when clicking on it.
    You can make this frog button a prefab and spawn it in the UI (by setting the parent of the prefab to the Canvas)

    Or if you want to stay in world space and work with tilemaps, sprites, animations, etc. you can use the method from the link above. You need a script on your frog that implements the interface. You can just copy the example from the link and rename the script to "Frog" and put it on your prefab. Then, when you spawn a frog and click on it, you will see an output on the console. And then you can put your "Ribbet" code in there. So every frog is clickable and does not need a button to follow. Of course, with that method, you need to do extra work to change the sprite if you want to but it's not hard to do.

    Let me know if you run into any issue.
     
    warrenbrandt likes this.
  5. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,883
    A simple solution could be to make your buttons world space UI, so you can make the button a child of your frogs in your prefab, and just hook things up normally via the inspector.
     
    Olipool and warrenbrandt like this.
  6. warrenbrandt

    warrenbrandt

    Joined:
    Mar 3, 2018
    Posts:
    413
    Spiney199 thats awesome, had it working in 5 minutes, thanks all, learned a lot.
     
    spiney199 likes this.