-2

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
      }
  }
}
Bagdan Gilevich
  • 1,231
  • 1
  • 11
  • 17
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Draco18s no longer trusts SE Aug 28 '18 at 03:56
  • @Draco18s I would not hurry in the duplicate case. First, your link is related to .NET, this is Unity and may be something different due to the DontDestroyOnLoad. – Everts Aug 28 '18 at 04:12
  • Check where the issue occurs, cannot be the property but more likely the playerData. I think you have too many static cases there and wrong naming (to my taste). Replace persistentData with Instance so it complies with Singleton naming. Then make playerData non-static. Finally, try PersistentData.Instance.playerData. Let's see where we going with this. – Everts Aug 28 '18 at 04:16
  • 1
    Also, based on the implementation of the IsSliding, you can reduce it to public bool IsSliding {get; set;} – Everts Aug 28 '18 at 04:18
  • @Everts Unity runs on .NET (it may be .NET 2.0 but its still .NET), there's nothing wrong with the duplicate target. The only thing that makes Unity different is *how* you get your object references not what to do when they're null (or how to diagnose which one is null). – Draco18s no longer trusts SE Aug 28 '18 at 14:32

1 Answers1

0

Your problem here is that you didn't have an object in your scene that contains PersistentData class.

Create an empty object and assign PersistentData.cs to it and try again.

Improvements:

  • You can get rid of isSliding in PlayerData.cs and change the getter and setter to this :

    public bool IsSliding { get; set; }

  • Since you are implementing a singleton for your PersistentData class, I would remove static keyword from PlayerData and in the ActionClass I would do:

    if (PersistentData.persistentData.playerData.IsSliding)
    {
    
    }
    

Happy coding!

Bagdan Gilevich
  • 1,231
  • 1
  • 11
  • 17
GhAyoub
  • 525
  • 4
  • 11