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
using UnityEngine;
using System.Collections;
public class HealthBarScript : MonoBehaviour {
public int Maximumhealth = 100;
public int CurrentHealth = 100;
public float healthBarLength;
// Use this for initialization
void Start () {
healthBarLength = Screen.width / 2;
}
// Update is called once per frame
void Update () {
AdjustCurrentHealth(0);
}
void OnGUI(){
GUI.Box(new Rect(10, 10, healthBarLength, 20),
CurrentHealth + "/" + Maximumhealth);
}
public void AdjustCurrentHealth(int adj){
CurrentHealth += adj;
if(CurrentHealth < 0){
CurrentHealth = 0;
}
if(CurrentHealth > 100)
{
CurrentHealth = Maximumhealth;
}
if(Maximumhealth < 1){
Maximumhealth = 1;
}
healthBarLength = (Screen.width / 2) * (CurrentHealth / (float)Maximumhealth);
}
}
And here is the MovementScript
var target : Transform; //the enemy's target
var moveSpeed = 3; //move speed
var rotationSpeed = 3; //speed of turning
var myTransform : Transform; //current transform data of this enemy
function Awake()
{
myTransform = transform; //cache transform data for easy access/preformance
}
function Start()
{
target = GameObject.FindWithTag("Player").transform; //target the player
}
function Update () {
//rotate to look at the player
myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);
//move towards the player
myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;
}
function OnCollisionEnter(theCollision : Collision){
if(theCollision.gameObject.name == "Player"){
}else if(theCollision.gameObject.name == "Golv"){
Debug.Log("Hit the wall");
}
}
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.