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

instantiating prefab at specific location

Discussion in 'Scripting' started by gibberingmouther, Oct 12, 2017.

  1. gibberingmouther

    gibberingmouther

    Joined:
    Dec 13, 2016
    Posts:
    259
    my inventory is basically a scroll bar object with a bunch of text fields and icons. i want to make it so when you right click, a menu (which is a prefab) is instantiated at the corner of the text field. how can i do this? i need to make it so it instantiates directly at the corner of the text field - so a specific offset from the text field.

    also do i need the prefab to include a canvas or can i instantiate it into an existing one? currently it is an image and four buttons with text fields and scripts so the text fields light up when i mouse over.
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You can just get a RectTransform's position and size (or sizeDelta - not quite sure at the moment). You could work with that in order to find the positions at which the corners are.

    As for the second question, of course you can instantiate it into an existing one. Of course it's always easier to visually arrange everything, but that's pretty much up to you.
     
    gibberingmouther likes this.