- Home /
join two scripts to use one var in both scripts
Hi i am trying lives to my game, i have it so when the good guy hits the bad guy but i can't get it so when it hits a different bad guy from a different script the same life var will go down by one. i know it is probable easy to do but i am new to this and i am only messing around with it. i do need it in C# because that is what i have done the game in and i don't really understand java thanks for your help :)
Answer by DoTA_KAMIKADzE · Apr 26, 2015 at 03:03 PM
You have generally 2 options:
1) Provide the same instance of class with that variable to each "bad guy" script. For example you can Find the same unique object on Awake function in all your "bad guy" scripts and store the reference to its script component which holds the actual value.
2) Just use static variable. And utilize it everywhere you need. So if your "bad guy" scripts are the same scripts or they inherit from same script you can add there that static variable, if they're not and doesn't inherit from same script then you can do so or make a custom holder class or add that variable to only 1 of that classes, I've described how to do something similar HERE.
Your answer
![](https://koobas.hobune.stream/wayback/20220613194109im_/https://answers.unity.com/themes/thub/images/avi.jpg)