Hello, I'm a bit new in C# and Unity and I've come across a problem with referring integers between two files. My code looks basically like (only those parts that are connected with my problem):
PlayerController.cs :
public Button AtakujBtn;
public int StartHP = 100, StartDMG = 10, MyHP = 100,
ZombieScript zombieScript;
void Start()
{
zombieScript = this.GetComponent<ZombieScript>();
Button Atakuj_button = AtakujBtn.GetComponent<Button>();
Atakuj_button.onClick.AddListener(AtakujZmb);
}
public void AtakujZmb()
{
zombieScript.ZombieAttack(ref StartDMG, ref StartHP, ref MyHP);
}
and ZombieScript.cs:
public GameObject InfoZmb, Zombie, ZombieAlive, ZombieDead;
public int ZombieHP = 100, ZombieBasicHP = 100;
public Text hpText, zombieHPText;
public void ZombieAttack(ref int StartDMG, ref int StartHP, ref int MyHP)
{
int PlayerATK = Random.Range(StartDMG * 2, StartDMG * 4);
ZombieHP -= PlayerATK;
zombieHPText.text = "Zombie HP: " + ZombieHP + "/" + ZombieBasicHP;
if (ZombieHP > 0)
{
int ZombieATK = Random.Range(10, 30);
MyHP -= ZombieATK;
hpText.text = "HP: " + MyHP + " / " + StartHP;
}
else if (ZombieHP <= 0)
{
ZombieAlive.SetActive(false);
ZombieDead.SetActive(true);
ZombieHP = ZombieBasicHP;
}
}
The error name in Console is:
NullReferenceException: Object reference not set to an instance of an object PlayerController.AtakujZmb () (at Assets/PlayerController.cs:86) ...
The 86 line is the one from PlayerController.cs -> zombieScript.ZombieAttack(ref StartDMG, ref StartHP, ref MyHP);
I know that the code is not perfect (really far from perfect) but I've just started learning and i want to learn how to pass values like those between files, thanks for your help!
PlayerController.cs :
public Button AtakujBtn;
public int StartHP = 100, StartDMG = 10, MyHP = 100,
ZombieScript zombieScript;
void Start()
{
zombieScript = this.GetComponent<ZombieScript>();
Button Atakuj_button = AtakujBtn.GetComponent<Button>();
Atakuj_button.onClick.AddListener(AtakujZmb);
}
public void AtakujZmb()
{
zombieScript.ZombieAttack(ref StartDMG, ref StartHP, ref MyHP);
}
and ZombieScript.cs:
public GameObject InfoZmb, Zombie, ZombieAlive, ZombieDead;
public int ZombieHP = 100, ZombieBasicHP = 100;
public Text hpText, zombieHPText;
public void ZombieAttack(ref int StartDMG, ref int StartHP, ref int MyHP)
{
int PlayerATK = Random.Range(StartDMG * 2, StartDMG * 4);
ZombieHP -= PlayerATK;
zombieHPText.text = "Zombie HP: " + ZombieHP + "/" + ZombieBasicHP;
if (ZombieHP > 0)
{
int ZombieATK = Random.Range(10, 30);
MyHP -= ZombieATK;
hpText.text = "HP: " + MyHP + " / " + StartHP;
}
else if (ZombieHP <= 0)
{
ZombieAlive.SetActive(false);
ZombieDead.SetActive(true);
ZombieHP = ZombieBasicHP;
}
}
The error name in Console is:
NullReferenceException: Object reference not set to an instance of an object PlayerController.AtakujZmb () (at Assets/PlayerController.cs:86) ...
The 86 line is the one from PlayerController.cs -> zombieScript.ZombieAttack(ref StartDMG, ref StartHP, ref MyHP);
I know that the code is not perfect (really far from perfect) but I've just started learning and i want to learn how to pass values like those between files, thanks for your help!