Hi there,
I am wanting to have a HUD item which will display ammo. So 12 bullets left, and I will use the 12 bullets left image as a texture. Regardless of how I do it tho, I am getting an error I can not explain.
No, GUI.Box has a type of void. You can never assign the result of a void typed function to a variable. You’re trying to treat GUI.Box like it returns a Rect when it doesn’t.
“Void” is nothingness. You can NEVER assign void to any variable. Note that void is not equal to null, and void is not equal to void, while null is equal to null.
The documentation clearly DOES NOT try to assign void to anything! But your line does.
Rect(…) creates a new instance of a Rect object which is passed to the GUI.Box method. No where in that example is the return value from GUI.Box(…) assigned to anything (because it does not return anything).
The Unity GUI system is kind of strange conceptually. You don’t get a reference to Box which you can then show/hide/etc. It is simply shown for every frame that GUI.Box(…) is called (OnGUI() is called every frame).
So you probably want something like this:
function OnGUI()
{
// These would probably be declared at class/file level and assigned in OnStart() or Editor
var hudItemRect1 = Rect(....);
var hudItemRect2 = Rect(....);
var hudItemRect3 = Rect(....);
if(count >= 1)
{
GUI.Box(hudItemRect1, texture1);
}
if(count >= 2)
{
GUI.Box(hudItemRect2, texture2);
}
if(count >= 3)
{
GUI.Box(hudItemRect2, texture3);
}
}