- Home /
how to solve -- Object reference not set to an instance of an object -- ?
At the end of my level there is a sign that when you touch it will bring you to the world map. In this script that is attached to the sign i activate a bool "LVL1_1_Completed "
Script on sign using UnityEngine; using System.Collections;
public class WinLVL1_1 : MonoBehaviour {
public bool LVL1_1_Completed;
void Start ()
{
}
void Update ()
{
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
PlayerHealth PlayerWins = other.gameObject.GetComponent<PlayerHealth> ();
PlayerWins.WinGame ();
LVL1_1_Completed = true;
}
}
}
On the canvas of the world map i have a script that must check if the bool is true .... if it is true the next level must be unlocked.
Script on world map using UnityEngine; using System.Collections;
public class UnlockLevels : MonoBehaviour {
public GameObject UnlockLVL_1_2;
void Start ()
{
}
void Update ()
{
if (GameObject.Find("SignLVL_1_1").GetComponent<WinLVL1_1>().LVL1_1_Completed)
{
UnlockLVL_1_2.SetActive (true);
}
}
}
When i hit play it gives me an error that says "Object reference not set to an instance of an object" in the line "if (GameObject.Find("SignLVL_1_1").GetComponent().LVL1_1_Completed)".
Please help me with this ;p
but if i do that then i must put my public game object also in there ... witch means i have to pu my next level symbol in there. I cnat do that because that elvel symbol is in the world map scene and the sign is in the lvl1 scene
i must make contact with the sign script "winlvl1" (the bool). I must get that from thatscript in my UnlockLevel script that is attached to my world map scene
Answer by Vagonn · Apr 26, 2016 at 05:18 PM
Put your
UnlockLVL_1_2.SetActive (true);
line to the end of OnTriggerEnter2D() method as below
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
PlayerHealth PlayerWins = other.gameObject.GetComponent<PlayerHealth> ();
PlayerWins.WinGame ();
LVL1_1_Completed = true;
UnlockLVL_1_2.SetActive(true);
}
}
Answer by aditya · Apr 27, 2016 at 04:59 AM
Obviously your code couldn't find SignLVL_1_1
please make sure that you are giving a correct name without any typos