- Home /
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.
Answer by Tomer-Barkan · Dec 15, 2013 at 06:47 AM
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.
$$anonymous$$aking the GUI controls children does not work well with GUITexture or GUIText, because their transform works very differently than world object transform. I'd say this is not such great design on Unity's side, but that's what we have.
You could make your character prefabs under the same empty gameobject, and make them one prefab, then you wouldn't need to create the connections.
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
lol, that's what I was trying to say in my original answer, but I edited anyway to make it clearer.
Your answer
![](https://koobas.hobune.stream/wayback/20220613124150im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
GUI Texture ontop of another GUI Texture 1 Answer
Screenshot PNG to GUITexture (NONE Texture) 0 Answers
Way to have a pause and play texture in same GUI texture? 0 Answers
How can i show the texture in the array? 2 Answers
OnGui Function help 1 Answer