NullReferenceException: Object reference not set to an instance of an object error ?
hello there , i making a Question and answer game in unity , but i get this problem whenever i try to check for the correct response , i have three scripts, the first one game_uGUI which basically set the questions and answers from the files , the second is call_win and the third is call_lose.
i have four buttons in the scene and everything is attached to its place scripts and everything
but one i test, it gives back : NullReferenceException: Object reference not set to an instance of an object game_uGUI.button_two_logic () (at Assets/mobile game menu kit/script/game_scene/game_uGUI.cs:414)
maybe my approach here is causing the problem as i am calling onMousDown() from both call_win& call_lose. this is game_uGUI script public void button_four_logic() { if (response_4.text.CompareTo(correct_rsp) == 0) { call_win.OnMouseDown(); } else call_lose.OnMouseDown(); }
public void button_one_logic()
{
if (response_1.text.CompareTo(correct_rsp) == 0)
{
call_win.OnMouseDown();
}
else
call_lose.OnMouseDown();
}
public void button_two_logic()
{
if (response_2.text.CompareTo(correct_rsp) == 0)
call_win.OnMouseDown();
else
call_lose.OnMouseDown();
}
public void button_three_logic()
{
if (response_3.text.CompareTo(correct_rsp) == 0)
{
call_win.OnMouseDown();
}
else
call_lose.OnMouseDown();
}
this is call_win script
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class call_win : MonoBehaviour {
public int star_score;
game_uGUI my_game_uGUI;
public int score;
public Text pscore;
void Start()
{
my_game_uGUI = GameObject.FindGameObjectWithTag("_gui_").GetComponent<game_uGUI>();
}
public void OnMouseDown ()
{
if (!game_uGUI.in_pause)
{
my_game_uGUI.star_number = star_score;
pscore.text = "100";
my_game_uGUI.Victory();
}
}
}
this is the call_lose script: using UnityEngine; using System.Collections;
public class call_lose : MonoBehaviour {
game_uGUI my_game_uGUI;
void Start()
{
my_game_uGUI = GameObject.FindGameObjectWithTag("_gui_").GetComponent<game_uGUI>();
}
public void OnMouseDown ()
{
if (!game_uGUI.in_pause)
{
my_game_uGUI.Defeat();
}
}
}
you might have to specify which button left click is if(Input.GetButtonDown("Fire1")) { right click is if(Input.GetButtonDown("Fire2")) {
Answer by toddisarockstar · Mar 16, 2016 at 02:40 AM
your error means that whatever gameobject variable your script is reffering to is empty. you are making your script try to check a gameobject variable that hasn't been filled yet.
but here i am just calling the function from call_win.on$$anonymous$$ouseDown(). Should i set both call_win and call_lose script to an object ?
well i attached both scripts to the buttons put i still have the same problem