Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by MilesAway1980 · Nov 18, 2015 at 12:44 AM · variablesclassstaticpublic

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gjf · Nov 17, 2015 at 08:27 PM 0
Share

even though you may think it irrelevant, you should show the structure/class for Orientation. why are you using a string for facing and not an enum? string comparisons are slow...

what do you mean by "I can't seem to find any way to set the public Sprite variables"?

avatar image MilesAway1980 gjf · Nov 17, 2015 at 08:33 PM 0
Share

I added the orientation class, but I really do believe it is irrelevant to the question. It's just a collection of flags so I can keep track of what the player is doing and assign the right sprite. The spring comparison is only a temporary thing I threw in there to get it work. I'll come up with something better later.

As for the original question, I have a static class with static public variables. Is there any way to set static public variables in a static public class from the inspector?

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ian_sorbello · Nov 18, 2015 at 01:27 AM

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);
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Nov 18, 2015 at 05:25 AM 0
Share

Nope, that won't work. You may ask why, because if your getSprite is static then you cannot use non static member within (faceLeft/Right).

I guess you meant getSprite to be non-static, then we're good. Also, I would recommend to use a DontDestroyOnLoad to make the object survive new scenes but then make sure to destroy any new instance that would duplicate.

avatar image MilesAway1980 · Nov 18, 2015 at 03:48 PM 0
Share

Very interesting idea, I hadn't thought of that.
Does the underscore have a specific property about it in C#/Unity? Or was that just a convention you like to use?

I'll play around with this and see what I can do with it. It may help me in other areas too.

avatar image OncaLupe MilesAway1980 · Nov 18, 2015 at 06:22 PM 0
Share

Underscores don't do anything special, so that's just their convention.

The problem with this, is that it requires an object in the scene with this script attached. Which means you need to make it a prefab singleton so every scene can have it. Also, as fafase mentioned, if the getSprite accessor is static it won't work as shown, just the instance variable/method should be static with a proper singleton setup in Awake.

avatar image ian_sorbello · Nov 18, 2015 at 08:08 PM 0
Share

Yes - agree - it does require an object in the scene with a script attached - but this is a strategy that has worked well for me.

And, it is a singleton, not a static class - hence why the static based Instance() method.

The reason why I think this is a handy pattern, is that your code can access the underlying assets through the getSprite() method (does not need to be static) - and the component itself is visible in the hierarchy window, so you can drag and drop your other assets (sprites) onto it easily.

The object is a dummy object - it doesn't need to be visible or be anywhere special.

If you make the object a prefab - then you can bring this into any scene you like...

avatar image fafase ian_sorbello · Nov 19, 2015 at 05:49 AM 0
Share

I modified the answer since it was wrong. getSprite cannot be static to work in the current context and to be used as you showed.

avatar image
0

Answer by OncaLupe · Nov 18, 2015 at 05:56 AM

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MilesAway1980 · Nov 18, 2015 at 03:49 PM 0
Share

I didn't know about these. I really like the idea of scriptable objects. The more I work in Unity, the less I like working in the Inspector and the more I like to script everything, so this is right up my alley. Thanks.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

I cant access a script that I made in Assets/Scripts from the FirstPersonCharacter script. 1 Answer

Managing In-Game Variables of Different Data Types 1 Answer

Game Objects not using same script independently. 1 Answer

Inheriting values from base class in c# 0 Answers

Static Class Strings not truly null? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges