Hi, I am making a 2D game and I wanted to access variables from class Buttons in class ValuesOfJanusz and vice versa. I did it writing:
public Buttons buttons = mainCamera.GetComponent<Buttons>();
And:
public ValuesOfJanusz voj = mainCamera.GetComponent<ValuesOfJanusz>();
But when I’m trying to load the game, I get this classic error:
Object reference not set to an instance of an object (in ValuesOfJanusz line 7, Buttons line 10)
I’ve tried many ways to solve the problem but I still have this problem.
What will you suggest?
I’m getting you full code:
Buttons:
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class Buttons : MonoBehaviour {
public static GameObject mainCamera;
//załączenie klasy ValuesOfJanusz
public ValuesOfJanusz voj = mainCamera.GetComponent<ValuesOfJanusz>();
//zmienne pasków
public bool ate;
public bool slept;
public bool healed;
//zmienne sterująca czasem
public bool done;
public int hour;
public Text time;
//lodówka
public GameObject fridgeInside;
public GameObject exitFridge;
public GameObject food;
public Text textOfFood;
public int countOfFood;
bool fridgeOpened;
//stół
public GameObject gazeta;
public GameObject ii;
//dymek Janusza
public GameObject dymek;
//////////////////////////////
void Start () {
done = false;
hour = 8;
ate = false;
healed = false;
slept = false;
fridgeInside.SetActive (false);
exitFridge.SetActive (false);
food.SetActive (false);
countOfFood = 3;
fridgeOpened = false;
gazeta.SetActive (false);
ii.SetActive (false);
dymek.SetActive (false);
}
////////////////////////////////
void Update () {
if (done) {
++hour;
time.text = hour.ToString() + ":00";
done = false;
if (hour == 23) {
hour = -1;
}
}
if(fridgeOpened){
textOfFood.text = countOfFood.ToString ();
}
}
//obsługa ekranu tytułowego
public void TitleButton(){
SceneManager.LoadScene ("House");
}
//otwieranie lodówki
public void FridgeClicked(){
fridgeInside.SetActive (true);
exitFridge.SetActive (true);
food.SetActive (true);
food.SetActive (true);
fridgeOpened = true;
}
//spanie
public void BedClicked(){
if (voj.sleep < 10) {
done = true;
voj.sleep++;
}
}
//jedzenie
public void FoodClicked(){
if (voj.hunger < 10) {
ate = true;
countOfFood--;
voj.hunger++;
}
}
//zamykanie lodówki
public void ExitFridge(){
fridgeInside.SetActive (false);
exitFridge.SetActive (false);
food.SetActive (false);
fridgeOpened = false;
}
//klikanie stołu
public void TableClicked(){
gazeta.SetActive (true);
ii.SetActive (true);
}
//czytanie gazety
public void GazetaClicked(){
done = true;
gazeta.SetActive (false);
ii.SetActive (false);
}
//robienie ii
public void doii(){
done = true;
gazeta.SetActive (false);
ii.SetActive (false);
}
}
ValuesOfJanusz:
using UnityEngine;
public class ValuesOfJanusz : MonoBehaviour {
public static GameObject mainCamera;
public Buttons buttons = mainCamera.GetComponent<Buttons>();
bool JanuszAction;
public int hp;
public int hunger;
public int sleep;
public int lvl;
public int exp;
void Start () {
hp = 10;
hunger = 10;
sleep = 10;
lvl = 0;
exp = 0;
}
void Update () {
//obsługa lvl
if (JanuszAction) {
exp++; JanuszAction = false;
}
if (exp == 100) {
lvl++;
exp = 0;
}
//obsługa pasków hp, hunger i sleep
if (buttons.ate) {
if (hunger < 10) {
hunger++;
buttons.ate = false;
}
}
if (buttons.healed) {
if (hp < 10) {
hp++;
buttons.healed = false;
}
}
if (buttons.slept) {
if (sleep < 10) {
sleep++;
buttons.slept = false;
}
}
/////////////////////
}
}