Search Unity

Add Image to Button

Discussion in 'Scripting' started by mholmes, Jan 27, 2020.

  1. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    I have a button that has a image inside it as a child. When you click the button, the child image follows your mouse cursor. the image following the mouse is later destroyed on click. I need to add a new image to the button. How do you add a child object in my case a new image to the button as a child? It needs to basically be a copy of the image originally inside the button.
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Why not just clone the image that is used to follow and turn off the one on the button? Or have a floating image that gets turned on and copies the sprite and that one follows the cursor?
     
  3. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    You have sample code? Not sure I understand. I get what your saying roughly just need clarification
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    No, don't really have sample code.

    You have a button with an image gameobject as a child.
    You have a floating image gameobject.
    Click on the button, copy the sprite from the child image component to the floating image component.
    Turn off the child. Turn on a floating image gameobject.
    The floating object should follow your cursor.
    When it's clicked on, turn off the floating object, turn on the child object which will have it's original sprite. Or if you need to, change it's sprite to whatever.

    If I misunderstood what you are trying to accomplish, please expand on your question.
     
  5. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    Ok that makes sense. I'll give that a shot tonight