GUI Texture button child of camera

Hi,

I’ve got a GUI texture of a button that I have in the bottom right of the screen and I’d like to child it to my camera but when the camera moves the GUI texture button moves out of view, I’d like to have it child of the camera so I can make a prefab of the camera with the GUI texture button attached.

I’ve tried using Camera.main.ViewportToWorldPoint() but cannot get to see the button, only if I don’t have it as child and set it’s position to (0,0,0)

Can someone point me in the right direction please, thanks.

1 Answer

1

GUI Texture’s position is always a fraction that represents it’s position relative to the screen. So if the position X is 0, it’s the leftmost part, 1 is the rightmost part, and 0.5 is the middle of the screen.

Making it a child kinda breaks this, as you can see in my question here.

I ended up solving this by using sprites instead of GUI Textures. But if you want to use GUI textures, either don’t make them children of anything, or make them children of an empty GameObject located in 0,0,0 and definitely don’t make them children of a moving object.

You could create an empty game object, that will hold both the camera and the GUI textures, and will always remain in 0,0,0, that way you can still instantiate them both at once with a single prefab.

Edit: Any objects that you need to connect to the GUITexture objects, you can also make a child of the same empty gameobject, and then they will all be part of the same prefab.

tbkn, thank you for the input, much appreciated. Thats what I'd thought it's not advisable to do what I wanted to do. I already have my GUI buttons child of a empty gameObject that stays a (0,0,0,) and all works fine, and also have it saved as a prefab, I also have my character who is controlled by the GUI controls saved as a prefab, so when I put my character and my GUI controls prefabs into the scene I have to make the connections again so I was looking to child the GUI controls to the character so all the connections are made and it's all saved as one prefab.

O dear .. :), you know I never even thought of doing it the other way round, like you say parent the character to the same empty game object as the GUI controls, that works, and like you say I don't have to make the connections, please convert your last comment to an answer so I can accept it as the correct answer, thank you