How can I change Parent object's position in Unity

Multi tool use
Multi tool use


How can I change Parent object's position in Unity



Image:
Imagee



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.

Z qsnRaM0,guwG,dcCfilYbrqOGxO2rMfXz,GqJY7YViCB 24 ipONoBdd9j61vTsrT,i0awz
P,K VZ41eanRgp,2XRCtd0rv3,o23,CJg VPJKRJ 0lvzx6IYusEdNmJA2GLTT8tPkbY

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

List of Kim Possible characters