How can I change Parent object's position in Unity

Multi tool use
How can I change Parent object's position in Unity
Image:
I am trying to make it move entire Inventory object
when dragging "NamePanel" object.
I added this script to "NamePanel" object
and it makes only NamePanel to move.
How should I edit my script to do that?
how can I get Parent's transform component?
since I'm not a English speaker, it's hard to get
information by only searching...
public class InventoryControl : MonoBehaviour, IBeginDragHandler, IDragHandler
{
/////////////////////////////////////////////////////////////////////////////////////
public Vector3 offset;
public void OnBeginDrag(PointerEventData eventData)
{
offset = transform.position - (Vector3)eventData.position;
}
public void OnDrag(PointerEventData eventData)
{
transform.position = (Vector3)eventData.position + offset;
}
//////////////////////////////////////////////////////////////////////////////////////
}
1 Answer
1
How should I edit my script to do that? how can I get Parent's transform component?
Use transform.parent
transform.parent
https://docs.unity3d.com/ScriptReference/Transform-parent.html
public void OnDrag(PointerEventData eventData)
{
transform.parent.position = (Vector3)eventData.position + offset;
}
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.