Draw a Rect using GUI.DrawTexture on exactly gameobject's position Unity
<p>I have to use the "GUI.DrawTexture(new Rect..." function to draw a rectangle on my screen(actually, it's a gradient GUI bar i got from <a href="https://www.assetstore.unity3d.com/en/#!/content/19972" rel="nofollow">https://www.assetstore.unity3d.com/en/#!/content/19972</a>, but deep inside it's just a rectangle). I want to do it right beside a gameobject i have on my screen.</p>

<p>The problem is: It seems like rectangle's coordinates are not the same as a gameobjects coordinates(i have searched online and i guess it's true). I have tried the function below to convert a gameobject's position to a rectangle's coordinates but still no luck:</p>

<pre><code> public Vector2 WorldToGuiPoint(Vector2 position)
     {
         var guiPosition = Camera.main.WorldToScreenPoint(position);
         guiPosition.y = Screen.currentResolution.height - guiPosition.y;

         return guiPosition;
     }
</code></pre>

<p>My best strike was when i did this:</p>

<pre><code>Vector3 gameObjectsPosition = Camera.main.WorldToScreenPoint(gameObject);
</code></pre>

<p>And then when i wanted to draw the rectangle i did:</p>

<pre><code>GUI.DrawTexture(new Rect(gameObjectsPosition.x, Screen.height - gameObjectsPosition.y, Background.width * ScaleSize, Background.height * ScaleSize), Background);
</code></pre>

<p>But still, the Rect isn't on the exact gameObject's position. It's x is right but y don't and when i searched the internet, they said I only had to do the "Screen.height - gameObjectsPosition.y" when drawing the Rect. It didn't work for me, the y is still wrong.</p>

<p>What should i do to create a rectangle that's like, right beside a current gameobject on screen (like, if the gameobject's position is x = -401 and y = -80, i want it on y=-80 and x=-300)</p>