Search Unity

Reset Image Object to oringal position

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

  1. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    414
    I have a image I set to follow my mouse using this tut:
    That park works great but now I need to reset the image back to its original location. I'm grabbing the original location on start using:

    <objectName>.transform.position But the issue I noticed was my image is inside a button object. So my image is a child object to my button parent. When I reset the position, its obviously not the correct location. I would like to try two options but not sure how to accomplish them.

    Option 1: Move the image back to the original position inside the parent button.

    Option 2: Destroy the image object and create a new one inside my parent object and position it in the original child objects position.

    Obviously option 1 is preferred. Little background on what Im trying to do. I have a hammer players will pick up when they click the button. They click on a tile to smash it and the hammer should return to the original location nested inside the button. Any help would be great. Thank you
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    The issue here is that RectTransform (used by all UI components) has more information than transform.position on its own can control. Try something like:
    Code (csharp):
    1. RectTransform rt = (RectTransform)transform;
    2. rt.anchoredPosition = ...
    3. rt.sizeDelta = ...
    Store those variables, and reassign their starting values to reset it. You might need .anchorMin and .anchorMax as well, I'm not sure.