- Home /
How to change pixel inset of GUITexture?
So when my health goes down I want the guiTexture to scale down in the x value just like a health bar. But I get errors when I try to get this to work. Can I keep it this way or do I have to change it?
Assets/Adventure Game/MaleCharacterPack/VincentSword_Control.cs(21,39): error CS1612: Cannot modify a value type return value of UnityEngine.GUITexture.pixelInset'. Consider storing the value in a temporary variable using UnityEngine; using System.Collections; public class VincentSword_Control : MonoBehaviour { public int health; public GameObject healthTexture; private bool isHit; private bool canPickupHealth; private bool isDead; // Use this for initialization void Start () { health = 100; isHit = false; isDead = false; } // Update is called once per frame void Update () { if(health == 100){ healthTexture.pixelInset.x = 200.0f; }if(health == 95){ healthTexture.pixelInset.x = 190.0f; }if(health == 90){ healthTexture.pixelInset.x = 180.0f; }if(health == 85){ healthTexture.pixelInset.x = 170.0f; }if(health == 80){ healthTexture.pixelInset.x= 160.0f; }if(health == 75){ healthTexture.pixelInset.x = 150.0f; }if(health == 70){ healthTexture.pixelInset.x = 140.0f; }if(health == 65){ healthTexture.pixelInset.x = 130.0f; }if(health == 60){ healthTexture.pixelInset.x = 120.0f; }if(health == 55){ healthTexture.pixelInset.x = 110.0f; }if(health == 50){ healthTexture.pixelInset.x = 100.0f; }if(health == 45){ healthTexture.pixelInset.x = 90.0f; }if(health == 40){ healthTexture.pixelInset.x = 80.0f; }if(health == 35){ healthTexture.pixelInset.x = 70.0f; }if(health == 30){ healthTexture.pixelInset.x= 60.0f; }if(health == 25){ healthTexture.pixelInset.x = 50.0f; }if(health == 20){ healthTexture.pixelInset.x = 40.0f; }if(health == 15){ healthTexture.pixelInset.x= 30.0f; }if(health == 10){ healthTexture.pixelInset.x = 20.0f; }if(health == 0){ healthTexture.pixelInset.x= 5.0f; } } void OnTrigger(Collider other) { if(other.gameObject.tag == "Enemy") { health = health - 5; } } } Assets/Adventure Game/MaleCharacterPack/VincentSword_Control.cs(21,39): error CS1612: Cannot modify a value type return value of
UnityEngine.GUITexture.pixelInset'. Consider storing the value in a temporary variable
Answer by Eric5h5 · Feb 08, 2014 at 02:10 AM
You can only do that in Unityscript. For C#, you have to do what the error says and make a temporary variable (a new Rect), modify it, then assign it again. Also, instead of having a million if statements, just use some math. It will make your code far shorter and more manageable. (For even better results, instead of constantly polling the health variable every frame in Update, only do that in a function where the health actually changes, such as the OnTrigger function.)
Answer by getyour411 · Feb 08, 2014 at 02:10 AM
I don't know what you are doing with that pixelInset thing but I recommend a new approach. Take a % of the health and use that % to set the width of the texture to display in OnGUI; you can replace all that stuff in Update() with something like this
public void calcHealthPct(float curHealth) {
curHealthPCT= (curHealth/maxHealth)*100f;
curHealthTextureWidth= ((curHealthPCT/100.0f)* maxHealthTextureWidth);
}
I don't use em but I assume OP can still apply math to there width or something? I use curHealthPCT to adjust the Rect() for my Texture2D.
It wasn't cut and paste, you have to change/match the variables to your project. Also as Eric5h5 points out, you are using GUITexture so you'll have to adopt using the new Rect() and assign it back.
I don't use GUITextures but Eric suggests you need to define a rect
Rect rect(new Rect(NewdesiredX,y,w,h));
and assign it; read the GUITexture doc
http://docs.unity3d.com/Documentation/Components/class-GuiTexture.html
Your answer
Follow this Question
Related Questions
How to make this GUITexture work. 1 Answer
Self Reference and TextMesh inside of a script to change the text 0 Answers
Change colour of guiTexture from green to red. 1 Answer
How come my Player, Enemy, Animation & HealthSlider won't correspond to one another? 1 Answer
how do u make diffrent chacters and give them commands 2 do things 2 Answers