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
1
Question by NightmarexGR · Sep 07, 2012 at 04:25 PM · javascriptgui

GuiTexture Width Change

Hallo i am bigginer at js and i made a js so i can use my damage scripts to reduse the Width of my GuiTexture as i have made it a healthbar. Here is my script but i dont know what's wrong :

 var HP : int = 100;
 
 function Start () {
     HP = guiTexture.texture.width;
 }
 function Update () {
     transform.width (HP);
 }



I would also want to make my player change location to 0.0.0 if HP or width is <= 0

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

1 Reply

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

Answer by aldonaletto · Sep 07, 2012 at 04:52 PM

That's totally wrong: you must get the GUITexture initial width from guiTexture.pixelInset.width and save it, then multiply by HP/100 and set it back at Update. But this script should be attached to the GUITexture object, which cannot be childed to the player! . Since the health bar can't be a child of the player, modifying the HP variable and respawn the player will be a bit tricky. A better alternative is to attach the health script to the player and use a reference to the GUITexture instead of the property guiTexture, like this:

  • Player health script:

// drag the GUITexture from the Hierarchy view to this field in the Inspector: var healthBar: GUITexture; var HP : int = 100; var lives: int = 3; // lives available var respawnPoint = Vector3.zero; // respawn position

private var width100: float; // the initial healthBar width

function Start () { width100 = healthBar.pixelInset.width; }

function Update () { // set the health bar width: healthBar.pixelInset.width = width100 * HP / 100; if (HP <= 0){ // player has died? if (lives > 0){ // if there are more lives... lives--; // decrement lives... HP = 100; // restore HP... transform.position = respawnPoint; // move to respawn point } else { // sorry, no more lives - game over! Application.Quit(); } } }

// function that applies the damage: function ApplyDamage(damage: float){ HP -= damage; } In the other script, you can apply damage in the collision event like this:

function OnCollisionEnter(col: Collision){
  col.transform.SendMessage("ApplyDamage", 5, SendMessageOptions.DontRequireReceiver);
}
Comment
Add comment · Show 3 · 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 NightmarexGR · Sep 07, 2012 at 06:11 PM 0
Share

You made me understand and i made my own health script based on your script, thnx dude !

avatar image NightmarexGR · Sep 07, 2012 at 06:35 PM 0
Share

so now i got an other problem i made 2 scripts one for Health and one for damage so in the first script i made a

Public var HealthPoints : int = 100;

and in the damage one i did this : var HealthDamage : int = 5;

 function OnCollisionEnter ()
  {
     HealthPoints : -= "HealthDamage";
 }
avatar image aldonaletto · Sep 07, 2012 at 10:32 PM 0
Share

It would be better to have a specific ApplyDamage function in the health script above, and call it explicitly or via Send$$anonymous$$essage in the collision event - take a look at my answer: I edited it to include the damage stuff.

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

9 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

How To Make GUI Buttons Load/Quit 1 Answer

How to have animation play correctly 1 Answer

I want my trigger sound only to play once! 0 Answers

Only In Range 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