Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
avatar image
23
Question by MythStrott · Aug 06, 2010 at 03:16 AM · prefabvariableinspectorstaticoafa

Is it possible to show Static Variables in the Inspector?

I am cleaning up the script in my game by moving all of the variables into one organized script. To make them available to all scripts I set the as "static var". It's been working/looking great so far.

Now I have a little problem. I use

var newObject : Transform;

for instantiating objects. I want to put all of these instantiatable object variables in my Variables script. However, when I set them as

static var newObject : Transform;

they vanish from the inspector. I need them to be in the inspector to set certain prefabs to these transform variables.

Is there some way I can make these viewable in the inspector? Or could I set prefabs within the script itself?

Thanks!

Comment
Add comment · Show 3
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 DeLong · Mar 17, 2012 at 12:12 AM 0
Share

Okay so he asked if it was possible and the question wasn't answered...is it possible?

avatar image Eric5h5 · Mar 17, 2012 at 12:16 AM 1
Share

The answer was implied in my answer. Namely, no.

avatar image DeLong · Mar 17, 2012 at 01:05 AM 0
Share

Yeah, I figured it out after I hit submit, but your comment isn't obvious at first as to why unless you are thoroughly familiar with the technique. After doing some reading and testing it made more sense. So for those who may stop by here later...here's the long version of Eric's comment...correct me if I am wrong.

The problem with static functions is you can't access non-static member properties of the class that contains the static functions. Why? Because although the functions are static and exist the member properties may not exist in memory until the class is instantiated. And if you try to make the class properties static so that you can access them anytime then you'll find that you can't see them in the Unity inspector. [head scratch time] The solution (as Eric5h5 recommended) is to create a singleton which have an "instance" property that points to itself and is instantiated on startup. With that solution you can now create public "non-static" member properties that will show up in the inspector (provided you inherit from $$anonymous$$onoBehavior) and you can now get at those member vars from within the static functions using the "instance" property.

10 Replies

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

Answer by Eric5h5 · Aug 06, 2010 at 03:29 AM

Use a singleton instead of making the variables static.

Comment
Add comment · Show 7 · 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 MythStrott · Aug 06, 2010 at 04:18 AM 0
Share

$$anonymous$$uchos Gracias. Thank you. Danke schn.

avatar image ina · Jan 23, 2012 at 06:07 AM 0
Share

isn't the singleton ultimately still a static var? http://www.unifycommunity.com/wiki/index.php?title=Singleton

avatar image Eric5h5 · Jan 23, 2012 at 07:23 AM 0
Share

The variables aren't static, no.

avatar image cregox · Sep 18, 2013 at 07:59 PM 0
Share

@ina indeed, singleton are "ultimately a static var" at all

avatar image MichaelDShark · May 30, 2016 at 07:33 PM 0
Share

How would you use a singleton? public bool smgT1Equipped;

Show more comments
avatar image
17

Answer by SasugaShogun · Jan 19, 2014 at 06:09 AM

It IS possible to see static variables in the inspector:

In the upper right hand corner of the inspector window there's a little lock button, next to that is an icon of a tiny arrow pointing down next to three lines.

Select the icon of the arrow & three lines, you'll get a drop-down menu. Select "Debug" instead of Normal and you'll be able to see static variables.

It can be very useful for seeing what's going on inside your scripts.

Comment
Add comment · Show 4 · 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 All_American · Aug 20, 2015 at 02:23 PM 4
Share

Did that...doesn't work

avatar image browne11 · Sep 24, 2015 at 04:23 AM 3
Share

Thank you for that little tip. I still can't see static variables with it on, but I'm super happy to see this debug mode. It makes it much easier to see where my issues, I suppose the title is self explanatory however they're so tiny I never saw them. ;p

avatar image Hsni · Nov 23, 2018 at 11:25 AM 1
Share

It doesn't show static variables but still is helpful in other ways.

avatar image Taylor-Libonati · Feb 25, 2019 at 09:58 PM 3
Share

This shows private variables not static variables.

avatar image
13

Answer by drudiverse · Mar 23, 2014 at 07:32 AM

Yes, use 2 variables, a one called staticvar and one called setstaticvar, and in function awake, make static var equals the figure you set in inspector. if necessary place the code in plugins or editor folder if you need it to compile early.

 #pragma strict
 
 var setmaxsteps = 50;
 static public var maxsteps = 50;
 
 var sete = 20;
 static public var e = 20;
 
 var setinverfaces = true;
 static public var inverfaces = true;
 
 
 function Awake(){
 maxsteps = setmaxsteps;
 inverfaces = setinverfaces;
 inverfaces = setinverfaces;
 
 }

i thought that singletons were way unintuitive to learn, the descriptions i found here and on net seemed to refer to making a new class and functions rather than static vars.

Comment
Add comment · Show 2 · 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 AmirSavand · Feb 22, 2018 at 06:02 PM 1
Share

That's interesting, but it's not best practice.

avatar image VengadoraVG · Apr 21, 2019 at 01:21 PM 0
Share

as @AmirSavand said, it's not best practice.

When you create a static variable, you make sure there is only one memory space for that variable. So if you create a ton of gameobjects with that script, you will always have only one shared memory space for that variable, and the variable will get shared across all of those game objects.

But if you create a local variable, only to have it rendered on the inspector, you are creating one memory space for each instantiated game object. This might seem like not a big deal, but if you are going to have a ton of instances, then this will be a concern, because you will also have a ton of memory spaces.

avatar image
2

Answer by geomorillo · Feb 16, 2013 at 06:37 PM

or you can use another variable to show its contents and if you modify it then the static variable also changes

 public int myinspectorvar =0; // you can initialize it with wathever value you want
 public int mystaticVar;
 public void Update(){
     mystaticVar = myinspectorvar;
 
 }

the only problem is when the static variable is changed from another script

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 A13x231 · Dec 27, 2016 at 03:18 PM 0
Share
  public int myinspectorvar =0; // you can initialize it with wathever value you want
  public static int mystaticVar {get; private set;}
  public void Update(){
      mystaticVar = myinspectorvar;
  
  }
avatar image
2

Answer by Shinugami · Mar 22, 2014 at 07:38 AM

As GeoMorillo said:

or you can use another variable to show its contents and if you modify it then the static variable also changes

and then he said:

the only problem is when the static variable is changed from another script

To overcome that problem you just make a function like this:

 function AdjustStatic( amount : int ){
  myInspectorVar += amount;
  // add both negative and positive such as 5 or -5
  // Use gameObject.transform.SendMessage("AdjustStatic", 5) to adjust the...
  // variable from another object. Only adjust the static value from inside the object.
 }

This is assuming you are updating the value :

 function Update(){
    mystaticVar = myInspectorVar ;
 }
Comment
Add comment · 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
  • 1
  • 2
  • ›

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

23 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Prefab's Script affecting all the Prefab 1 Answer

Public variable not showing in Inspector(Solved) 1 Answer

Can I use strings with static variables? 2 Answers

How does one inspect static vars? 3 Answers

How can i assign prefab to variable without drag & drop from project to inspector. 2 Answers


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