- Home /
How To Call Health Script From Another Script ?
hello i have An Empty gameobject i added health bar script to it as u can see in the picture it is called ( HealthBar )
#pragma strict
public var health : float; // var helath
var texture1: Texture2D; // texture 2d
var mat : Material; // material
var x : float = 0;
var y : float = 0;
var w : float;
var h : float;
function Start () {
health = 1000; // begining health is 100
}
function OnGUI(){
if(Event.current.type.Equals(EventType.Repaint)){ //Is the game being drawn?
var box : Rect = new Rect(x, y, w, h); // the postion of the drawn on screen
Graphics.DrawTexture(box, texture1, mat); // you draw the bo and texture2d and material
}
}
function Update () {
var healthy : float = 1-(health/100);
if(healthy == 0){
healthy =.1;
}
mat.SetFloat("_Cutoff", healthy);
if ( health <= 0 )
{
GameOver() ;
}
}
function GameOver ()
{
Application.LoadLevel("GameOver") ;
}
function AddHealth ()
{
health +=30 ;
}
and that is the code of HealthBar Script this Script Called ( HelathBar1) what i want is to make the health Float Increase By ( 30) if a cube hit a bullet
the problem that when the bullet hit the cube nothing happen first i want to ask ? where should be the script who call the health bar script on ? on bullet or on the cube ? secound how to call the health bar and increase the health of it by ( 30 ) and that is my trying but it is not working
#pragma strict
private var player : GameObject ;
private var HealthScript : HealthBar1 ;
function Start () {
//var HealthScObject = GameObject.FindGameObjectWithTag ("Player");
//if (HealthScObject != null)
// {
// HealthScript = HealthScObject.GetComponent ("HelthBar1");
// }
// if (HealthScript == null)
// {
// Debug.Log ("Cannot find 'GameController' script");
// }
}
function OnTriggerEnter (other : Collider)
{
if ( other.gameObject.tag == "Player")
{
other.GetComponent(HealthBar1).health += 30 ;
}
}
it is increase the health by ( 30 ) when it is Hit the HealthBar Empty Object who have the script (HealthBar1 )
if added Health bar script to the bullet it is working also so what should i do to call it with out added it to the bullet hope you help me
Answer by Wisteso · Jun 14, 2014 at 09:01 PM
Generally speaking the code needs to do something like this... (C# version)
SomeClass sc = GameObject.Find("ObjectWithSomeClass").GetComponent<SomeClass>();
sc.SomeMethod();
Your answer
Follow this Question
Related Questions
screenshot 0 Answers
Android Development; How to use SDK manager! 1 Answer
Distribute terrain in zones 3 Answers
Calling an android method from Unity3D with a JAR plugin 0 Answers
Procedural Spawning Theorems 0 Answers