- Home /
Lower player's health from separate script.
So basically, i want to make it so that when the enemy object touches the player object, it will lower the player's health. The problem is that, the enemyAI script is written in javascript and the health bar script is written in c#. I could either get the enemy script to modify a variable or call a function in the health bar script (which would modify the variable value anyway). How would i go about doing this? Let me know if you need to see my scripts or anything.
Thanks in advance-
Answer by FortisVenaliter · May 14, 2015 at 05:20 PM
There are a number of answers on this site and on the forums and on google detailing how to access one variable from one language in another. Those are pretty easy to find if you search.
That being said, though, if you want to avoid headaches in the long run, use only one scripting language.
Answer by InsertFunnyUsernameHere · May 14, 2015 at 05:29 PM
Why would you even write scripts in Javascript and C# at the same time? That doesn't make sense unless you're working in a group... Anyways if you want the player to loose health by simply touching the enemy then you may want to use OnCollisionStay or OnCollisionEnter functions and access the collider's tag to check if it's a player.
For example
public class HealthScript :MonoBehaviour
{
float health
{
set
{
health=value;
if(health<0)
Die();
}
}
public static void ReceiveDamage(float damage) {
health-=damage; }
voidDie() { Destroy(this); }
}
public class Enemy :MonoBehavior
{
float damage;
OnCollisionStay(Collision col)
{
if(col.collider.gameObject.tag=="player")
{
HealthScript.ReceiveDamage(damage);
}
}
}
Your answer
Follow this Question
Related Questions
What is this C# code in javascript 0 Answers
GetComponent, What is it ? 1 Answer
summing up items price in runtime 1 Answer
Changing Value in JavaScript from C# 1 Answer