- Home /
Positioning UI objects on 2D Game Objects
I know many people have asked questions about this before and I've checked out a lot of answers and tutorials but forgive me, I still can't get my head around positioning 2D UI Images with script. I'm trying position a UI Image on my UI to the position of a UI Image within the game, everything is 2D using Unity's GUI system.
UI and game are created in two seperate scenes with two seperate cameras. I can position images on the UI to other objects also within the UI, but not reliably to objects in the game. I'm currently trying this (c#):
GameObject PlaceHold = GameObject.FindGameObjectWithTag(placehold);
Vector3 thisPosition = uiCam.ScreenToViewportPoint(PlaceHold.transform.position);
mainSprite.transform.position = thisPosition;
Where the object within the game is tagged placehold (that bit works fine, it finds the tagged object), mainSprite is the UI Image I'm trying to position and uiCam is the camera of the UI. No errors, and the UI object is moved but not near to the PlaceHold game object.
I've also tried simply:
mainSprite.transform.position = PlaceHold.transform.position
and this works for some objects, but not all. I've also tried using the RectTransform of the objects without success, am now getting quite confused by it. Thanks for any answers!
Your answer
Follow this Question
Related Questions
How do i get the UI button position? 2 Answers
Scroll view/rect broken on Android only 0 Answers
Positioning New UI Elements In 4.6 At Runtime 1 Answer