change position of another gamobject based on current

Hello,

I have a gameobjects that user can move in 2D world. I have add several additional buttons around this objects so user could control size and rotation.

rotateButton.GetComponent<RectTransform>().position = new Vector2(Input.mousePosition.x, Input.mousePosition.y + 74);

My first problem is that magic number ‘74’. It looks nice in editor but terrible in build. How I can replace it in relative offset to gameobject?

My second problem is that when i scale object my mouse is on control button so i can’t position control panel based on mouse position as in code above. This results in control menu overlap with gameobjects. How to make them scale / change offset based on another gameobjects size (scale) or rotation.

Thanks!

Hello,

I have found a solution. You need to make object(s) as child of objects that will be changed (resized in my situation) and this changes will be applied to them as well. Very neat.

Here is example:

resizeButton.transform.SetParent(this.transform, false);

Little explanation:

this.gameObject

This will use gameObject of current object that it’s attached to.

resizeButton

Another gameObject that will become a child of ‘this.gameObject’. You can see in Editor during Playing mode that child game object will be under the parent object in Hierarchy tree.

P.S. If you want to make an offset of child in respect to parent you need to use ‘localPosition’.

Good luck!