Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by FPSworrior · Feb 08, 2014 at 01:45 AM · guitexturehealthbarhealth-deduction

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.)

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FPSworrior · Feb 08, 2014 at 07:53 PM 0
Share

Thanks for the help I ended up figuring it out.

avatar image
0

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);
 }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Eric5h5 · Feb 08, 2014 at 02:11 AM 0
Share

The code isn't using OnGUI, it's using a GUITexture.

avatar image getyour411 · Feb 08, 2014 at 02:15 AM 0
Share

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.

avatar image getyour411 · Feb 08, 2014 at 02:38 AM 0
Share

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.

avatar image getyour411 · Feb 08, 2014 at 04:56 AM 0
Share

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

avatar image FPSworrior · Feb 08, 2014 at 07:53 PM 0
Share

Thanks for the help I ended up figuring it out.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges