- Home /
Instantiate on value change
I've search an answer for this simple question but didn't found something really helpful. In my project I have a script with a simple int variable "Damage" that store the amount of Damage the character receive everytime he gets hit by a trap. I just want to instantiate a damage text each time his "Health" value change. I thought this would be really easy, something like Damage.OnValueChange... But I can't figure out how to do that !
Thanks for the answers I'm trying to make this work
Please don't post an answer to your own question like this. If you need to say something like this, it should be a comment responding to the relevant post. Also be sure to select the correct answer as "accepted" :)
Answer by bpaynom · Aug 29, 2018 at 01:47 PM
You can use System.Action
from msdn C# like this:
public class MyClass : MonoBehaviour
{
public Action onHealthChange;
public int myHealth = 5;
public void AddHP ( int amount)
{
myHealth += amount;
if ( amount != 0 && onHealthChange != nulll )
onHealthChange.Invoke();
}
}
Then in another class you could do:
public class AnotherClass : MonoBehaviour
{
public MyClass mc;
void OnEnable()
{
mc.onHealthChange += MyFunction;
}
void OnDisable()
{
mc.onHealthChange -= MyFunction;
}
void MyFunction()
{
Debug.Log("Health changed");
}
}
You can also make onHealthChange static, and then just suscribe to that event like MyClass.onHealthChange += MyFunction;
I guess he could, but that's so much more coding than necessary all just to instantiate...
Yeah, I know. I just gave him this solution because that's what I undestood he wanted. Ofc it's not the best solution.
I was just wondering if I use msdn System.Action, will it work on Android platform ?
It'll. Don't worry. You can also use UnityEvent if you prefer.
This is a good answer because it uses some high level structures that come built right into the language. It answers the question the OP asks... But I think perhaps not the answer the OP needs... Using high level structures that one doesn't understand clearly when they program it can get them into trouble if you use this solution, be sure you understand what's going on with this because you don't want to have to relearn it all over again in a month, when you go to add a feature or debug it.
Answer by myzzie · Aug 29, 2018 at 01:31 PM
His health value changes when he takes damage, call your function from there.
I think this is the best answer. This seems like the most obvious and clear solution. It's important that code be clear and consistent. Code consistency and clarity is always my #1 priority. OP probably doesn't realize there's extra overhead processing to onchange system actions.
Answer by tormentoarmagedoom · Aug 29, 2018 at 01:49 PM
call the funciton to instantiate at the same place where you modify the health from the damage taken...
Answer by DDeathlonger · Aug 29, 2018 at 01:31 PM
It would depend on where you apply the damage to the health. just write the line:
Instantiate([yourGameObject], transform2Inst, Quaternion.identity) as GameObject;
somewhere inside the same method.
Your answer
![](https://koobas.hobune.stream/wayback/20220612173721im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Instantiate on value change 2 Answers
My Photon health script doesnt work 2 Answers
Photon Combat script does not work 0 Answers
(C#) Enemy health and take damage from bullets 1 Answer
Take health from enemy 3 Answers