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 Chappy · Nov 05, 2013 at 06:50 PM · c#meshhealth

Changing Mesh of an object depending on Health Value. C#

I'm trying to set up code that will cause the mesh of the player to change depending on how much health they have. I believe that I'm supposed to use MeshFilter.mesh but I'm not completely sure how to do so.

Code I'm using:

 public class PlayerHealth_Test : MonoBehaviour {
     
 public float CurrentHealth = 100.0f;
 public GameObject Player;
 private float MaxHealth = 100.0f;
 public bool MeshChange = false;
 public Mesh NoDam;
 public Mesh LowDam;
 public Mesh MedDam;
 public Mesh HighDam;
     
 void Start()
     {
         Player = GameObject.FindGameObjectWithTag("Player");
     }
 
     
     void Update () {
     if (CurrentHealth > 100)
         {
             CurrentHealth = MaxHealth;
         }
     if (MeshChange == true);
         {
         if (CurrentHealth == 100.0f)
             {
                 Mesh = GetComponent<MeshFilter>(NoDam).mesh;
             }
         
         if (CurrentHealth <= 75.0f)
             {
                 Mesh = GetComponent<MeshFilter>(LowDam).mesh;
             }
         if (CurrentHealth <= 50.0f)
             {
                 Mesh = GetComponent<MeshFilter>(MedDam).mesh;
             }
         if (CurrentHealth <= 25.0f)
             {
                 Mesh = GetComponent<MeshFilter>(HighDam).mesh;
             }
         
         }
     if (CurrentHealth <= 0)
         {
         GameObject.DestroyObject(Player);
         }
     
     }
 }


The only errors right now are in the mesh assigning lines of the if statements because, again, I don't really know how to use MeshFilter.mesh. I've looked through the script reference but looking over the example code doesn't really give me a really clear idea on how to use it.

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
1
Best Answer

Answer by rutter · Nov 05, 2013 at 07:36 PM

Sometimes it's easier to switch GameObjects altogether, as described here.

Past that, you're probably looking for something more like this:

 if (MeshChange) {
     Mesh myMesh;
     if (CurrentHealth > 75f) {
         myMesh = NoDam;
     } else if (CurrentHealth > 50f) {
         myMesh = LowDam;
     } else if (CurrentHealth > 25f) {
         myMesh = MedDam;
     } else {
         myMesh = HighDam;
     }
     GetComponent<MeshFilter>().mesh = myMesh
 }

Keep careful track of your data flow: if you're picking one Mesh from a set, and then assigning it to a MeshFilter, your code should read like you're doing just that.

Watch your function calls: there's no version of GetComponent() that takes arguments past the type requested.

Comment
Add comment · Show 2 · 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 Chappy · Nov 05, 2013 at 07:50 PM 1
Share

Thanks for the help, that seems to have worked. I'll need to wait to meet up with the people I'm working with to double check though.

Also, thanks for the advice, I'm rather new to coding so I'm surviving on what I can find on the script reference and youtube so most the really technical (exactly how GetComponent() works/what it's limits are for example) stuff just goes over my head right now. Also, I'll remember that GameObject switching just in case.

avatar image rutter · Nov 05, 2013 at 10:10 PM 0
Share

There's a steep learning curve in most areas of game dev, but you're asking smart questions at least! Best of luck.

avatar image
0

Answer by CodeKenny · Nov 05, 2013 at 07:24 PM

In this case I would probably create 4 textures (or MORE) to represent the health left and swap those ( GUI elements ) according the damage / healing received by the player. And if it looks too bad, make more textures and change them with higher frequency.

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

17 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

Related Questions

Multiple Cars not working 1 Answer

Public Health Var C# 1 Answer

Distribute terrain in zones 3 Answers

Enable/Disable Touch during runtime. 0 Answers

Procedural mesh generation, problem with textures 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