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 Ramtown · Sep 05, 2012 at 10:55 PM · guienemyattacktargettinghealth

Show only selected enemy's health bar

'Ello all. I've been 'aving a bit of an issue with the above. I kind of wonder if I'm just trying to access the variable the wrong way.

'ere's where I'm trying to access it in the targetting script:

     private void SelectTarget()
     {
         //We'll change the selected target's color to red, so we can tell it apart.
         selectedTarget.renderer.material.color = Color.red;
         
         EnemyHealth2 st = (EnemyHealth2)GetComponent("EnemyHealth2");
         st.GetComponent<EnemyHealth2>().isSelected = true;
         
         PlayerAttack2 pa = (PlayerAttack2)GetComponent("PlayerAttack2");
         
         pa.target = selectedTarget.gameObject;
     }
     
     private void DeselectTarget()
     {
         //We'll change the color back to white.
         selectedTarget.renderer.material.color = Color.white;
         selectedTarget = null;
         
         EnemyHealth2 st = (EnemyHealth2)GetComponent("EnemyHealth2");
         st.GetComponent<EnemyHealth2>().isSelected = false;
     }

'ere's the bit containing the variable in my enemy 'ealth script. The bool is declared up top and initially set to false.

 void OnGUI()
     {
         if(isSelected = true)
         {
             //Set up a box to hold our health bar.
             GUI.Box(new Rect(10, 40, healthBarLength, 20), curHealth + "/" + maxHealth);
         }
     }
     
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
Best Answer

Answer by aldonaletto · Sep 05, 2012 at 11:06 PM

You're doing a big mess when accessing the selected target EnemyHealth2 script. You must have a reference to the selected target in order to get one of its components - and selectedTarget is this reference:

 private void SelectTarget()
 {
    //We'll change the selected target's color to red, so we can tell it apart.
    selectedTarget.renderer.material.color = Color.red;
    // use selectedTarget as the GetComponent reference:
    selectedTarget.GetComponent< EnemyHealth2>().isSelected = true;
    // without a reference, the owner object is assumed - in this
    // case, PlayerAttack2 must be attached to the same object as
    // this script:
    PlayerAttack2 pa = (PlayerAttack2)GetComponent("PlayerAttack2");
    pa.target = selectedTarget.gameObject;
 }

 private void DeselectTarget()
 {
    //We'll change the color back to white.
    selectedTarget.renderer.material.color = Color.white;
    selectedTarget.GetComponent< EnemyHealth2>().isSelected = false;
    selectedTarget = null; // assign null after using selectedTarget!
 }
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 Ramtown · Sep 06, 2012 at 07:09 PM 0
Share

Thanks. I've inputted the code, but it's doing the same as before I made the changes you gave (showing all enemy 'ealth bars, not just selected. When attacking one, you can see a bar go down behind, 'owever you can see at least one still full in front of the depleted.).

Am I messing something up in the 'ealth script (I can post the rest if needed), or is it perhaps because I have the same 'ealth script attached to several enemy objects, and it's thus setting all to true (or something similar)?

avatar image aldonaletto · Sep 06, 2012 at 10:17 PM 0
Share

The script attached to an object is an independent instance: each one has its own variables, which aren't altered when other instances of the same script are modified (except static variables). But I suspect that your problem is in the OnGUI code, in this line:

    if (isSelected = true)

This if is always true, because "=" is the assignment operator: any assignment in JS and CS returns the value assigned, thus this if will always see a true! You should use "==" ins$$anonymous$$d, or just:

    if (isSelected)

You definitely don't need to compare a boolean variable to true: this comparison always returns the variable value!

avatar image Ramtown · Sep 06, 2012 at 10:27 PM 0
Share

Alright, thanks. Can't believe I messed that one up... Just goes to show 'ow rusty I've really become, I guess :P

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

8 People are following this question.

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

Related Questions

Deduct health on collision 2 Answers

Weapon and enemy health 0 Answers

I want to attack me enemy and decrease their health, but t won't work, what am I doing wrong with my script? 4 Answers

Melee Damage script by collision 2 Answers

Rect Following Object : Unity3d 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