Here’s a little script I’m using to display a preview image (a Texture2D) when mousing over a menu item.
The Unity Editor displays an error “Trying to set null texture on a GUITexture”, which is no surprise, since I’m setting the GUITexture to null (which is its initial value) when mousing off. It still works fine, but is there a more recommended way to set a GUITexture texture to “None”?
var doc:Texture2D;
var gui:GUITexture;
private var oldTexture:Texture;
function Start() {
oldTexture = gui.texture;
}
function OnMouseEnter () {
if (gui!= null doc != null) {
oldTexture = gui.texture;
gui.texture=doc;
}
}
function OnMouseExit () {
if (gui != null doc!=null) {
gui.texture=oldTexture;
}
}
I guess when one sets the .texture property to null means “don’t draw”, and since you can easily do .enabled=false to achieve that same effect, Unity warns you because setting the texture to null is something you may be doing accidentally.
When fixing the bug, I thought that maybe it does not make sense to set texture to null, hence I added the warning message. But yeah, it should probably be removed.
When texture is set to null, the GUITexture won’t be drawn.
That being said, you script code will be cleaner if you just disable the component. If you set the texture to null, you need code to replace it with the real value when you turn it back on
OK, I changed the code to disable the GUITexture instead of setting its texture to null, and that works just as well and without the warning message.
I think it’s debatable whether it’s cleaner - it’s the same number of lines, or maybe one extra (the OnMouseEnter code still has to set the texture property each time because there are multiple menu items each running this script with a different preview texture to be displayed in that GUITexture), and I would argue that it’s not so clean to allow the initial value to be null and yet disallow or discourage setting it to null. Not a big deal, though, as long as I can achieve the intended effect! Thanks.
I am doing my Inventory with these GUITextures and i need to swap the textures all the time.
If the Slot is empty i am trying to set the texture value to null.
I think this way is cleaner for me, because otherwise i have to check the enabled status all the time.
Is there any otherway then disabling the component without an error message?
well, what’s the best way to release the texture so it can be garbage collected then?
I need to swap textures on a guiTexture and sometimes during the process there is simply no texture I want to display.
So settings guiTexture.texture=null I was expecting it would free the memory of that texture, but after seeing this thread I’m not sure anymore. Is it garbage collected if I set it to null or it will still be there referenced?
This is important because I’m on a mobile device, and it makes a big difference.
I was wondering about the “Trying to set null texture on a GUITexture” warning too.
One way to get around the warning and reduce texture in memory is create a 32 x32 px blank image will full transparency. Set it to RGBA PVRTC 2 bit, mipmap turned off. Load this into the GUItexture. The “blank” texture ends up being 256 bytes.
I don’t know if turning off the guitTexture actually releases texture from memory, so I replace it with the blank texture first, turn it off, and use Resources.UnloadUnusedAssets to be on the safe side.