Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Itaden · Sep 25, 2011 at 10:36 AM · translate

Help with translating health script from java to C#

Hi Guys

relatively new to unity and began with java script but now want to change to C#. I previously had a health bar 2d texture set up which would change size with the following javascript code which worked;

 var maximumHitPoints = 100.0;
 var playerHealth = 100.0;
 var damage : int = 50;
 var healthGUI : GUITexture;

function LoseHealth () {

 playerHealth -= damage;
 
     UpdateGUI();

 

function UpdateGUI(){

         var healthFraction = Mathf.Clamp01(playerHealth / maximumHitPoints);
     
       
         healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;
     
     
     
     }

I have changed to the following C# code

 using UnityEngine;
 using System.Collections;
 
 public class PlayerHealthGuiScript : MonoBehaviour {
     
     public float maxHealth;
     public float currentHealth;
     public int damageAmount;
     public GUITexture healthBar;
     private float healthGuiWidth;
 

void Start () {

         healthGuiWidth = healthBar.pixelInset.width;
         
     
     }
     
 

public void LoseSomeHealth () {

         currentHealth -= damageAmount;
             UpdateGui();
         
         
         
              
         
     }

void UpdateGui () {

     var healthFraction = Mathf.Clamp01(currentHealth / maxHealth);
 
     healthBar.pixelInset.xMax = healthBar.pixelInset.xMin + healthGuiWidth * healthFraction;
 }

I now get this compilation error

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



Any help greatly appreciated

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

3 Replies

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

Answer by Bunny83 · Sep 25, 2011 at 03:22 PM

Setting xMax in this case doesn't make much sense since it's much more complicated that way. xMin, yMin, xMax and yMax are just wrapper properties and they work internally (kind of) with x, y, width and height.

Like already been mentioned in JS when you access the member of a value type property it automatically creates a local variable, change the member and assign it back. It might be easier but break the rule of value types since it behaves like a reference type.

All you have to do is:

 Rect newInset = healthBar.pixelInset;
 newInset.width = healthGuiWidth * healthFraction;;
 healthBar.pixelInset = newInset;
 


***********************************
ps just in case someone wants to know how xMax and xMin are implemented:

 public float xMax
 {
     get
     {
         return this.m_Width + this.m_XMin;
     }
     set
     {
         this.m_Width = value - this.m_XMin;
     }
 }

 public float xMin
 {
     get
     {
         return this.m_XMin;
     }
     set
     {
         float xMax = this.xMax;
         this.m_XMin = value;
         this.m_Width = xMax - this.m_XMin;
     }
 }
Comment
Add comment · 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
2

Answer by Reiv3r · Sep 25, 2011 at 03:04 PM

 int maximumHitPoints = 100;
 int playerHealth = 100;
     float healthGUIWidth;
 GUITexture healthGUI;
 void LoseHealth(int damage) {
     if(playerHealth - damage > 0)
         playerHealth -= damage;
     else
         playerHealth = 0;
 }
 void OnGUI() {
     float healthFraction = Mathf.Clamp01(playerHealth / maximumHitPoints);
         
         Rect rect = new Rect();
         rect.xMin = healthGUI.pixelInset.xMin;
         rect.xMax =  healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;
 
         healthGUI.pixelInset = rect;
 }

Try this. Modify as you see fit.

Comment
Add comment · 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
0

Answer by Itaden · Sep 26, 2011 at 01:12 AM

Thanks all of you I tried all your solutions and they seem to work fine. Not only that but I now have a better understanding of what was going wrong as well which will help in the future.

I have another 7 scripts to convert so you may hear from me again :)

Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

c# to JavaScript how? 3 Answers

How to fix this for c#? 1 Answer

Need this translated. Js to C#. 2 Answers

[SOLVED] Camera jitters while moving towards/away from player 2 Answers

Translate not working properly 1 Answer


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