My Script:
#pragma strict
import UnityEngine.UI;
import UnityEngine.Networking;
var MaxHealth = 100;
var Health : int;
var Healthbar : UnityEngine.UI.Scrollbar;
var Hungerbar : UnityEngine.UI.Scrollbar;
var Thirstbar : UnityEngine.UI.Scrollbar;
var MaxHunger = 100;
var MaxThirst = 100;
var Thirst : int;
var Hunger : int;
var thirstIsZero = false;
var hungerIsZero = false;
public static var zombieskilled = 0;
var zombieskilledtext : Text;
@SerializeField
var dontDrawLayer : String = "DontDraw";
@SerializeField
var PlayerGraphics : GameObject;
function Start ()
{
Healthbar = GameObject.Find("HealthBar").GetComponent(Scrollbar);
Hungerbar = GameObject.Find("HungerBar").GetComponent(Scrollbar);
Thirstbar = GameObject.Find("ThirstBar").GetComponent(Scrollbar);
Healthbar.size = 1;
Health = MaxHealth;
Hungerbar.size = 1;
Thirstbar.size = 1;
Hunger = MaxHunger;
Thirst = MaxThirst;
zombieskilledtext = GameObject.Find("Zombieskilled").GetComponent(Text);
}
function Update ()
{
zombieskilledtext.text = "Zombies Killed: " + zombieskilled.ToString ();
Hungerbar.size = Hunger / 100f;
Thirstbar.size = Thirst / 100f;
if(Hunger <= 0)
{
Debug.Log("Player Dying");
hunger();
}
if(Thirst <= 0)
{
Debug.Log("Player Dying");
hunger();
}
if(RespawnMenuV2.playerIsRespawned == true)
{
Health = MaxHealth;
Hunger = MaxHunger;
Thirst = MaxThirst;
RespawnMenuV2.playerIsRespawned = false;
}
//if(!isLocalPlayer)
//{
//setLayerRecursively(PlayerGraphics, LayerMask.NameToLayer(dontDrawLayer));
//}
}
function ApplyDammage (TheDammage : int)
{
Health -= TheDammage;
Healthbar.size = Health / 100f;
if(Health <= 0)
{
Dead();
}
}
function Dead()
{
RespawnMenuV2.playerIsDead = true; //VERY IMPORTANT! This line was added in tutorial number 19. If you haven't reached that tutorial yet, go ahead and remove it.
Debug.Log("Player Died");
Hungerbar.enabled = false;
Thirstbar.enabled = false;
Healthbar.enabled = false;
}
function hunger()
{
Health -= 10.0;
yield WaitForSeconds(5);
Health -= 10.0;
yield WaitForSeconds(5);
Health -= 10.0;
yield WaitForSeconds(5);
Health -= 10.0;
yield WaitForSeconds(5);
Health -= 10.0;
yield WaitForSeconds(5);
Health -= 10.0;
yield WaitForSeconds(5);
Health -= 10.0;
yield WaitForSeconds(5);
Health -= 10.0;
yield WaitForSeconds(5);
Health -= 10.0;
yield WaitForSeconds(5);
Health -= 10.0;
yield WaitForSeconds(5);
}
//function SetLayerRecursively ()
//{
//var obj : GameObject;
//var newLayer : int;
//obj.layer = newLayer;
//for (var child : Transform in obj.transform)
//{
//SetLayerRecursively(child.gameObject, newLayer);
//}
//}
The error:
NullReferenceException: Object reference not set to an instance of an object
PlayerStatsV2.Update () (at Assets/Scripts/PlayerStatsV2.js:43)
please tell me how to solve this.