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 Stealthygolem · Nov 06, 2014 at 08:02 PM · c#variablevariablestextmeshcall

Call a method whenever the assigned variable gets altered

Hello! I am working with damage numbers (and health pickups) to be shown on screen. I have the code working to show me the number with a juicy random instantiate and a delayed fade. So, I don't need any help with the text mesh instantiation itself. What I want to know, is if I can call my "TakeDamage()" method, which contains my DamageNumber text mesh assignment every time the enemy takes any form of damage? The last sentence would logically be something like:

 //Whenever curhp gets reduced, call TakeDamage();

here is some visual aiding code to help you understand what I'm looking for:

     public float curhp;
     public float hpLost;

       void Update () {
          //if curhp altered: call TakeDamage()
          //the variable "hpLost" is assigned damage through other scripts,
          //in case you were wondering
       }

       void TakeDamage() {
         objectSpeed = 60f;
         Vector2 randomDirection = Random.insideUnitCircle * objectSpeed;
         GameObject dicks = Instantiate(textMeshObject,transform.position,Quaternion.identity) as GameObject;
         
         TextMesh text = (TextMesh)dicks.GetComponent(typeof(TextMesh));
         text.text = "-" + hpLost.ToString();
         dicks.rigidbody2D.AddRelativeForce(randomDirection);
     }
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
2
Best Answer

Answer by Serinx · Nov 06, 2014 at 08:28 PM

You can use Properties or "Getters and Setters" to achieve this.

The syntax is as follows:

     private float curhp;
     
     //Curhp is a basic property
     public float Curhp
     {
         get
         {
             //Some other code
             return curhp;
         }
         set
         {
             TakeDamage();
             curhp = value;
         }
     }

For your scenario I think you would only want to call TakeDamage in the set.

References:

http://unity3d.com/learn/tutorials/modules/intermediate/scripting/properties

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 Stealthygolem · Nov 06, 2014 at 08:45 PM 0
Share

I didn't expect it to be so easy. After doing some "Googleresearch" I stumbled upon the Observer method. Which did not really seem that simple. Overcomplicated considering what I was asking.

I still need to understand the getter and setter a bit better! Thanks for your help, much appreciated.

avatar image Serinx · Nov 06, 2014 at 08:52 PM 0
Share

The link I added explains properties quite well

avatar image
-2

Answer by timdan · Nov 06, 2014 at 08:46 PM

Create a variable called lasthp, and give it the value of carhop at the end of every update. You can then use it to check to see if curhp has changed since the last frame. (I'm also assuming you have a way of updating "curshp" as I didn't notice it in your code.

Like such:


 public float curhp;
 public float lasthp;
 public float hpLost;
 
 void Update ()
 {
    if(curhp < lasthp)
       TakeDamage();
 
    lasthp = curhp
 }

//Rest of your code here from example

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

28 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 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

Distribute terrain in zones 3 Answers

Variable not showing update in inspector 2 Answers

How to use variable in MailMessage To? 1 Answer

Noob c# question 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