How do I make a gui texture appear in front of other gui objects?

I am drawing a GUITexture at the mouse location to show an item that has been picked up. All works well, except the GUITexture is always drawn BEHIND all other GUI elements and I need it to be on top always. I have tried setting the z transform both positive and negative on the GUITexture object, with no effect. How do you set GUI draw depth if not by z depth?

Thanks, David

Using DrawTexture (as suggested by Jaap) is probably a better way to handle what you're trying to do than using a GUITexture, if I understand the intent. I'd like to add that setting GUI.depth to a very low number (like 0) just before calling DrawTexture will put it in front of other GUI elements. If you want your texture to follow the mouse cursor, you can do the following:

var itemTexture : Texture2D;    

function OnGUI () {
    GUI.depth = 0;
    GUI.DrawTexture (Rect (Input.mousePosition.x - (itemTexture.width / 2),
                           Input.mousePosition.y - (itemTexture.height / 2),
                           itemTexture.width,
                           itemTexture.height), 
                     itemTexture);
}

"The depth of each layered GUI Texture is determined by its individual Z Transform position, not the global Z position." <-- taken from unity's manual.

Draw the GUITexture using GUI methods, example:

var tex : Texture;

function OnGUI () {
    GUI.DrawTexture(Rect(0, 0, 100, 100), tex);
}

(of course, don't forget set the tex property to the render texture in inspector).

The order which they are drawn is the order in which you call them in the function, from bottom to top. If you have the controls in 2 separate components, then you need to manually assign them to different layers to specific the order, otherwise they'll draw in an arbitrary order. OnGUI gets called after all the other rendering (opaque, transparent, water, pfx, etc), so the layers order you use for GUIs only affects their draw order with respect to one another. (i.e. if you're feeling lazy, just set the layer of what you want to draw on top to be 'Water')

It really seems so easy but why can’t i make it work?

function GuiBox ( counter : int, mileStone : int, MScheck : int, padding : float, firstText: String, secondText : String, reward : int )
{

var claimed : int = 0;

GUI.color.a=0.5;

if ( counter >= mileStone ){

MScheck = 1;

GUI.color.a = 1;

Activate( padding, newMark );

}

GUI.Button( Rect( Screen.width * position.boxX,
Screen.height * position.boxY * padding,
Screen.width * position.boxWidth,
Screen.height * position.boxHeight),
firstText + mileStone + secondText+"
Reward: " + reward );

}

function Activate ( padding : float , newMark : Texture )
{
	GUI.depth = 0;					// here i set depth to zero
	GUI.DrawTexture( Rect(Screen.width 		* position.checkX, 
						  Screen.height		* position.checkY * padding, 
						  Screen.width 		* position.checkWidth, 
						  Screen.height 	* position.checkHeight),
						  newMark,   								//Check Mark Texture
						  ScaleMode.ScaleToFit , true, 0);
						 
}

those buttons are in a scrollView btw.
i tried GUI.depth = -1000 to 1000.

Simply set the Z position on the Transform component. Negative moves forward, positive moves backwards. Remember, Z position, not rotation nor scale. If you have attached a GUI texture onto a gameobject, editing this in Unity menu would be easier.

Um sorry for this late reply, but the answer could not be more simple :slight_smile:
Lets say you have a text and a texture :slight_smile:
If you put the text after the texture in the code then it will display above
Example Code:

GUI.DrawTexture(new Rect(40,40,98,100), texture);
GUI.Label(new Rect(68,72,80,20),"Text");