- Home /
How do you call from another script?
I have two extremely basic scripts. One for hunger and one for health in my game. I am having trouble calling functions from the hunger script into the health script. I am pretty new to coding but i can usually understand code just fine. (see scripts below)
Line 35 is where i think i need to call to the hunger script
#pragma strict
var MaxHealth = 100;
var Health : int;
var timer : float;
function Sart ()
{
Health = MaxHealth;
}
function ApplyDammage (TheDammage : int)
{
Health -= TheDammage;
if(Health <= 0)
{
Dead();
}
}
function Dead()
{
RespawnMenuV2.playerIsDead = true;
Debug.Log("Player Died");
}
function RespawnStats ()
{
Health = MaxHealth;
}
function Hungery()
{
//place where i think i need to call from the Hunger Script
timer+=Time.deltaTime;
if(timer>20)
{
Health-=1;
healthreset();
}
}
function healthreset()
{
timer=0;
}
Line 22 is where the hunger(); is called (im sorry i couldnt separate the two scripts)
#pragma strict
var Hunger:int;
var MaxHunger:float=2000;
var timer:float;
function Start ()
{
Hunger=MaxHunger;
}
function Update ()
{
timer+=Time.deltaTime;
if(timer>20)
{
Hunger-=1;
hungerreset();
}
if (Hunger<0)
{
Hungery(); //i have placed this function in the health script
}
}
function hungerreset()
{
timer=0;
}
function OnGUI ()
{
GUI.Box(Rect(900,550,85,55),"Hunger"+Hunger);
}
Answer by KRanges · Apr 21, 2014 at 01:15 AM
If you are calling to another script there are a few different methods in unity, you can create an instance of that script and access its members that way, or you can use SendMessage . Google or unity forums search for things like this, this is stuff explained in the basic unity tutorials and can be found easily.