|
Hey, I am trying to access a variable in a script called HealthBarScript. HealthBarScript is attached to a gameObject called Player, whilst i want to access in a script called EnemyMovementScript wich is on a gameObject called Enemy. Here are the scripts. Here is the HeathbarScript And here is the MovementScript I want to access CurrentHealth in the EnemyMovementScript at the bottom function called OnCollisionEnter. How would i do that? What i want to happend is that i want them to collide and the health si withdrawn by like 5 or something.
(comments are locked)
|
|
Like @syclamoth said, the problem here is the different languages: JS and CS can't see each other during compile time. There are a few ways to solve this, but since you need only a one-way communication path, I think the best alternative is to use SendMessage to call a function in the CS script that modifies the health.
void AddToHealth(int value){ // add value to the health
CurrentHealth += value; // (or subtract, if the value is negative)
}
If both scripts are attached to different objects, you must have in the JS script a reference to the CS object - transform, gameObject, collider etc.
var healthObject: Transform; // drag the health bar object here
function DecreaseHealth(){ // this function reduce health by 5
healthObject.SendMessage("AddToHealth", -5);
}
The other alternatives are:
(comments are locked)
|
|
This... isn't helpful. As far as I can tell, it's a problem with one being in C# and the other being in JS.
Nov 15 '11 at 09:25 AM
syclamoth
In that case http://unity3d.com/support/documentation/ScriptReference/index.Script_compilation_28Advanced29.html
Nov 16 '11 at 04:59 AM
DaveA
(comments are locked)
|
