- Home /
Button as child object
Hello. I have a little problem with my game object. I want to have a couple of child objects and this childs should be buttons. In specify: if I have 2 cubes and I click on one of them 2 buttons should appear (childs of this cube) and when I clik on another one for example 3 buttons should appear (another buttons). The problem is when I add a buttons as childs they move and rotate with parent. I added following code to button:
void Start () {
rotation = transform.rotation;
position = transform.position;
}
// Update is called once per frame
void Update () {
transform.rotation = rotation;
transform.position = position;
}
This code only prevent buttons to not move but they still rotate. Is there any other way to prevent this child buttons form moving and rotating with their parent? Thank you very much for your help
Why would you want them to be children of the cubes, if they won't move with the cubes? If you don't want them to move with the cubes, don't make them children. Just position them near the cubes...
Because I want to have different pack of buttons depends on which object I click. I thought the easiest way to get it is create buttons as childs and when I click on object no.1 then (for example) button1 and button2 should appear. But when I click on object no.2 then buttons 1 and 2 should disappear and buttons 3, 4 and 5 should show. Should I just add this buttons normally and connect them with objects by script?
If i was going to do this, i was simply going to reference the UI Buttons to the script, that will show \ hide them and enable \ disable them, when the appropriate action happens. Parenting them to the objects, that you want to click (To activate the buttons) won't help you in any way, except if you want to search for some children, but that is slow, unless you have 1000 objects, that you want to click on...
Your answer
![](https://koobas.hobune.stream/wayback/20220612093044im_/https://answers.unity.com/themes/thub/images/avi.jpg)