error CS1612: Cannot modify a value type return value of `UnityEngine.GUITexture.pixelInset'. Consider storing the value in a temporary variable

Hello

I have a C# script. I am trying to display 5 different variables on 5 different textures. These variables are Damage, FameMultiplier, Speed, Tires and Handling. My code is ` void Update () {

	SpeedT.guiTexture.pixelInset.width = (Speed * pixelToLiveRatio1);
	DamageT.guiTexture.pixelInset.width = (Damage * pixelToLiveRatio2);
	FameMultiplierT.guiTexture.pixelInset.width = (FameMultiplier * pixelToLiveRatio3);
	TiresT.guiTexture.pixelInset.width = (Tires * pixelToLiveRatio4);
	HandlingT.guiTexture.pixelInset.width = (Handling * pixelToLiveRatio5);

	if (carSelected == 0)
	{
		Speed = 152/404;
		Damage = 35/100;
		FameMultiplier = 20/100;
		Tires = 25/100;
		Handling = 35/100;
	}

	if (carSelected == 1)
	{
		Speed = 162/404;
		Damage = 28/100;
		FameMultiplier = 60/100;
		Tires = 35/100;
		Handling = 50/100;
	}

	if (carSelected == 2)
	{
		Speed = 155/404;
		Damage = 32/100;
		FameMultiplier = 22/100;
		Tires = 29/100;
		Handling = 42/100;
	}`

The error that I keep on getting is error CS1612: Cannot modify a value type return value of `UnityEngine.GUITexture.pixelInset’. Consider storing the value in a temporary variable.

From searching the web and from personal testing I can tell that while it works in Javascript it doesnt work in C#.

Any advice will be appreciated

Can’t set the Rect that way

Replace the top part with:

Rect speedRect = SpeedT.guiTexture.pixelInset;
speedRect.width = Speed *pixelToLiveRatio1;
SpeedT.guiTexture.pixelInset = speedRect;

Rect damageRect = DamageT.guiTexture.pixelInset;
damageRect.width = Damage * pixelToLiveRatio2;
DamageT.guiTexture.pixelInset = damageRect;

Rect fameMultiplierRect = FameMultiplierT.guiTexture.pixelInset;
fameMultiplierRect.width = FameMultiplier * pixelToLiveRatio3;
FameMultiplierT.guiTexture.pixelInset = fameMultiplierRect;

Rect tiresRect = TiresT.guiTexture.pixelInset;
tiresRect.width = Tires * pixelToLiveRatio4;
TiresT.guiTexture.pixelInset = tiresRect;

Rect handlingRect = HandlingT.guiTexture.pixelInset;
handlingRect.width = Handling * pixelToLiveRatio5;
HandlingT.guiTexture.pixelInset = handlingRect;

It’s because C# passes structs by value, not by reference. When you request the pixelInset field, you are getting a copy of that struct; modifications made to the copy do not affect the original.

You can resolve this by declaring a temporary variable:

Rect tempRect = SpeedT.guiTexture.pixelInset;
tempRect.width = (Speed * pixelToLiveRatio1);
SpeedT.guiTexture.pixelInset = tempRect;

If you’ll be doing this often, you may want to write a helper function and/or extension method.