Cannot convert void to void.

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.

function OnGUI()
{
	var hudItem1 = GUI.Box(Rect(hudItemRect), texture1);
	if(count == 1)
	{
		hudItem1 = GUI.Box(Rect(hudItemRect), texture1);
	}
	if(count == 2)
	{
		hudItem1 = GUI.Box(Rect(hudItemRect), texture2);
	}	
}

Error 1: line 3
Assets/scripts/ttGUIHudTest.js(22,13): BCE0125: Invalid declaration type ‘void’.

Error 2: line 6 and 9
Assets/scripts/ttGUIHudTest.js(25,35): BCE0022: Cannot convert ‘void’ to ‘void’.

Scratching head. How can I not convert a void to void? Seems inexplicable to me.

20 bucks says your variable hudItemRect is declared as a Rect()… in other words, you can probably get away with
GUI.Box(hudItemRect,texture1);

You can’t assign the result of GUI.Box because it’s type is void.

@Thatbadguy

var hudItemRect : Rect;

No good?
It is what I am using for GUI buttons and it works.
:.?

@KelsoMRK

Does that mean it is preassigned or something?

I tried declaring the var as

var hidItem1 : GUI.Box() : void;

but still an error of syntax.

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.

I get what you are saying tho i do not grasp, fully. Void yet. Is it relevant in js?

And this is from the ref scripts.

JavaScript
// Draws a box of the size of the screen.
function OnGUI() {
     GUI.Box(Rect(0,0,Screen.width,Screen.height),"This is a title");
}

I see a Rect used. So what is it that is different about mine?

“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);
	}
}

Right.
I see what I was doing.

Thanks!!!

← This guy just got himself 20 bucks :stuck_out_tongue:

Say whuuuuttt?

Edit.
Oh, right, i gots yah.

lol what for?

you wernt correct.

I don’t know… looks about right, no? :face_with_spiral_eyes: