Setting public static variables in a static class

Hi all,
I’m not finding anything that seems to address this issue:

I’m creating a static class for holding all of the sprites for players. That way, I only have to assign the sprites once and any player can get them by accessing the static class.

example:

public static class PlayerSprites {
    public static Sprite faceLeft;
    public static Sprite faceRight;

    public static Sprite getSprite(Orientation orientation)
    {
        if (orientation.facing == "left")
        {
            return faceLeft;
        }
        else
        {
            return faceRight;
        }
    }
}

Orientation is simply a class that holds a series of ways the character can behave:

public class Orientation {
    public string facing = "left";
    public bool standing = true;
    public bool shooting = false;
}

}

However, I can’t seem to find any way to set the public Sprite variables. Is there a way to do this?

Your entire class does not need to be static - just the accessor, i.e. getSprite().

Something like this would work:

public class PlayerSprites : Monobehaviour {
     public Sprite faceLeft;
     public Sprite faceRight;
     private static PlayerSprites _instance;

     void Start() {
         _instance = this;
     }

     public static PlayerSprites Instance() {
         return _instance;
     }

     public Sprite getSprite(Orientation orientation)
     {
         if (orientation.facing == "left")
         {
             return faceLeft;
         }
         else
         {
             return faceRight;
         }
     }
 }

You can then add this component (it’s now a MonoBehaviour) on an object in your hierarchy. Then use Unity to drag and drop the Sprites you like into the public variables.

Then finally, you can get access to your sprites like this:

Sprite s = PlayerSprites.Instance().getSprite(orientation);

I’d look into Scriptable Objects. They’re items that stay in your Assets, but can hold data like a script. You can access the data from any script in your scene like a Static, but the contents are visible in the inspector as well. Also, since it’s an asset, any changes made to it in runtime are saved. They take a little to set up, but should work perfectly for what you want.

http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/scriptable-objects