I created a PlayerData
object to store all variables for the player that I will need throughout other scripts. I am having an issue with an accessor returning a bool value and I don't know why it's giving me this error. It won't give me errors setting the bool but only returning it and I don't see why since the object was declared in the main class. Error:
NullReferenceException: Object reference not set to an instance of an object.
MAIN CLASS
public class PersistentData : MonoBehaviour {
public static PersistentData persistentData;
public static PlayerData playerData;
void Awake ()
{
if (persistentData == null)
{
DontDestroyOnLoad(gameObject);
persistentData = this;
playerData = gameObject.AddComponent<PlayerData>();
}
else if (persistentData != this)
{
Destroy (gameObject);
}
}
}
PLAYER DATA CLASS
public class PlayerData : MonoBehaviour {
private bool isSliding;
public bool IsSliding
{
get
{
return isSliding;
}
set
{
if (value == true || value == false)
{
isSliding = value;
}
else
{
isSliding = false;
}
}
}
}
CLASS THAT CALLS THE OBJECT
public class ActionClass : MonoBehaviour {
void LateUpdate()
{
if (PersistentData.playerData.IsSliding)
{
//CODE SHOULD EXECUTE BUT GIVES NULLREFERENCE ERROR
}
}
}