Answer the question
In order to leave comments, you need to log in
What is the correct way to assign parent object after Instantiate in unity?
Good evening!
Such a task: I am working now with drag & drop through an event trigger, it is necessary that at the beginning of the drag a copy of the object is created, in a certain branch of the hierarchy, which will follow the cursor.
I do it like this:
public void onModelBeginDrag(GameObject model){
movedModel = Instantiate (model as GameObject);
movedModel.transform.SetParent (wall1.transform);
public void onModelDrag(){
movedModel.transform.position = Input.mousePosition;
}
}
Answer the question
In order to leave comments, you need to log in
Input.mousePosition returns the mouse coordinates in pixels and depends on the screen resolution. Depending on the CanvasScaler settings, these coordinates may not match the canvas's local coordinates. To get the correct mouse position, use RectTransformUtility.ScreenPointToWorldPointInRectangle and RectTransformUtility.ScreenPointToLocalPointInRectangle . And it's even better not to access the mouse directly, but to use the event system through IDragHandler , then the coordinates will always be correct, the documentation even has an example with drag and drop.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question