[C#] Passing a value to pixelInset.xMax struct

I’m in the process of converting a heap of .js scripts to C#, and have been running into a bunch of syntax issues when trying to directly pass a value into a struct.

Here’s the line of JavaScript code that I’m trying to convert (decreasing a health bar GUI):

// - Adjust maximum pixel inset based on it
		healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;

In C#, I get the following error:

Assets/Scripts/PlayerBehaviour.cs(118,27): error CS1612: Cannot modify a value type return value of `UnityEngine.GUITexture.pixelInset’. Consider storing the value in a temporary variable

I’m at an absolute loss as to how to create a temporary variable in order to set the xMax value to the above formula.

As an aside, I kludged through a solution for a similar issue when trying to pass a value to a Color struct. I would be grateful if someone could take a look at the conversion and confirm and/or laugh at whether I am on the right path:

Original JavaScript:

// Start off by fading out GUI by setting alpha channel to 'clear'
	guiTexture.color.a = 0;

C# ‘fix’ to get rid of “storing in temporary variable” error:

// Start off by fading out GUI by setting alpha channel to 'clear'
		Color tempTextureClear = new Color(0,0,0,0);
		guiTexture.color += tempTextureClear;

Any help would be greatly appreciated! :slight_smile:

You may try something like this:

Rect pos = healthGUI.pixelInset;
pos.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;
healthGUI.pixelInset = pos;

Note: I did not test these statements.

MightyMao’s code looks good.

This really stems to how the languages handle structs in the context of property getters/setters. Since pixelInsert is a getter/setter, when you access it a copy of the Rect is returned, not the original Rect itself. C# handles this strictly so modifying the copy is meaningless so it throws an error. UnityScript hacks it by changing the code to create a new instance, modify it, then reassigns it through the setter which to me is scary (especially if no setter is defined).

EDIT: Perhaps one quick tweak to MightMao’s code would be:
pos.xMax = pos.xMin + healthGUIWidth * healthFraction;
if only to avoid hitting the pixelInsert getter a second time.

MightyMao and FizixMan, you are both legends! Thank you for your code tips and explanations.

Coming from a non-programming background and learning C# for the first time, UnityScript just seems a lot more straightforward and noob-friendly (like in this example, using only one line vs. having to break it down into a separate variable in C#), but I’ve heard that C# is easier to debug in the long run. Plus, it feels like I’m learning more about programming, no matter how hair-pulling it can get at times…

I’ve noticed you guys have been actively posting help a lot - is there a rep/give candy system? You guys sure deserve it. :smile:

C# might have a higher learning curve, but at least once you learn it you got your foot in the door to developing against the entire .NET framework (ASP.NET, Silverlight, C#.NET, WPF, Winforms, etc.)

No problem about the help, I’m just bored. Plus I talk out of my ass more often than not. >.> There are other posters here which are legends.

I guess C# has a wider application, but all I really want to do is make games… but hey, you never know. Maybe Silverlight will actually take off and become the de facto gaming platform sometime in the distant future.

A future with delicious, flying bacon… mmmmm…

Silverlight 5 was just recently announced with an actual hardware accelerated 3D API so likely we’ll see more gaming in its future (especially if the Windows Series Phone 7 Series Phone Name OS Thing takes off). I might be biased being a Silverlight developer myself, but there’s nothing else like it in the web-application world – I’d never go back to Flash/Java/JavaFX/JavaScript if I had the option – so I hope it never goes away!

Though I doubt it’d become the de facto gaming platform – that’s not its true purpose (right tool for the right job); I’d rather see the focus continue to be on LOB apps. (perhaps if only because that’s what I currently use it for >.>)

Did you mean about my Kung Fu? Then I agreed with you. About my Unity legend, I disagreed. Anyway, I am glad that I am able to help you.