• 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 leo77748 · Apr 27, 2012 at 03:15 PM · guidamage

How to use apply damage?

Okay I've got a battery gui but i want it so that when i click on an object that it would add to the battery and also update the gui as well.

Here is the code I have.

var remaining : float = 0; var pos : Vector2 = new Vector2(20,40); var size : Vector2 = new Vector2(60,20); var battery : Texture2D; var energy : Texture2D;

function OnGUI() { GUI.DrawTexture(Rect(pos.x, pos.y, size.x, size.y), battery); GUI.DrawTexture(Rect(705, 355, (size.x-15)- (45 *Mathf.Clamp01(remaining)), size.y-10.5), energy); }

function Update() { remaining=Time.time *0.01; }

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
0

Answer by chris gough · Apr 27, 2012 at 11:32 PM

not sure if you want battery image to fill up as you collect more batteries or just a new battery image to show up representing how many you have, the script below is for filling up as you collect more:

place t$$anonymous$$s wit$$anonymous$$n your current script:

static var batteryAmount : int;

var batteryState1 : Texture2D;

var batteryState2 : Texture2D;

var batteryState3 : Texture2D;

function OnGui(){

if (batteryAmount == 1){

GUI.DrawTexture(Rect(pos.x, pos.y, size.x, size.y), batteryState1);

}else if (batteryAmount == 2){

GUI.DrawTexture(Rect(pos.x, pos.y, size.x, size.y), batteryState2);

}else if (batteryAmount == 3){

GUI.DrawTexture(Rect(pos.x, pos.y, size.x, size.y), batteryState3);

}

}

you get the idea.

for the collection if you want it on click create a new script on place it on the object you want to collect.

function OnMouseOver(){

if (Input.GetButtonUp(0){

name of original script.batteryAmount++;

}

}

that should work, i t$$anonymous$$nk. any problems let me know

(there maybe some mistakes to the actual writing as i just wrote it then =P)

Comment
Add comment · Show 6 · 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 leo77748 · Apr 30, 2012 at 02:34 PM 0
Share

Actually, I wanted it so it fills up as I collect more batteries.

avatar image chris gough · Apr 30, 2012 at 05:39 PM 0
Share

this is for that you will need separate images of different states. so the first image will be empty the next might be a 1/4 full etc.

avatar image leo77748 · May 01, 2012 at 03:29 PM 0
Share

Yeah I know that but can you help me with filling it up as I collect more batteries?

avatar image AlucardJay · May 01, 2012 at 03:40 PM 0
Share

the above script takes care of the battery state's image (though it could do with formatting @ chris gough =] )

You need to provide more information. How are you collecting the batteries? Is it a mouse-click , or a trigger / collision event? And is the collection of the battery happening on the sasme script , or on the battery script (if it has one)?

More info please =]

avatar image leo77748 · May 03, 2012 at 03:15 PM 0
Share

I'm collection the batteries through a mouse click and it's happening on a different script.

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How To Make Floating Combat Text In new UI System of 4.6 1 Answer

2D GUI Group moving with players. 1 Answer

Pop up text from Floating text positioner interface c# 0 Answers

Health Bar Only For Falling Damage 2 Answers

need help with GUI healthbar 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