Search code examples
c#unity-game-engineunityscript

Create GUI UI at GameObject position


I want to create a GUI UI at the position of a GameObject, specifically this example menu:

http://docs.unity3d.com/Manual/gui-Basics.html

I've tried this:

new Rect (playerPosX, playerPosY, 100, 90)

as the first argument using gameObject.transform.position.x and y but the GUI is top-left based.

How do I do this?


Solution

  • WorldToScreenPoint will do the trick.
    Unity Documentation

    var pos = Camera.main.WorldToScreenPoint(GameObject.Find("NameOfObject").transform.position);
        Rect d = new Rect(Screen.width - pos.x, Screen.height - pos.y, 400, 400);