Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Digital-Phantom · Apr 08, 2015 at 07:19 AM · variableinspectorstaticintpublic

Public variable not showing in Inspector(Solved)

I had a quite simple script that kept count of the number of enemies in my scene and when that reached zero it loaded the next scene. Now I have multiple scenes I needed to change the variable number in the inspector for each scene rather than have a new script each time.

But for some reason the changing the variable to a public one has not let it show up in the inspector?

 using UnityEngine;
 using System.Collections;
 
 public class LevelCleared : MonoBehaviour
 {
 
     public static int enemyNumbers = 10;
 
 
     void Awake()
     {
         //enemyNumbers = 21;
     }
     
     void Update()
     {
         if(enemyNumbers <= 0)
         {
             var i = Application.loadedLevel;
             AutoFade.LoadLevel(i+1 ,2,1,Color.black);
             //Parameter 1 - LevelName or LevelIndex
             //Parameter 2 - FadeOutTime - time in seconds until the level actually starts loading
             //Parameter 3 - FadeInTime - time in seconds until the fade-in process is completed.
             //Parameter 4 - FadeColor - this is the color the screen fades to.
         }
     }
 }
 

The variable was originally a static in, now public static. The commented out line in the awake function is just a left over from when there was a single scene.

I'm getting no compiler errors and the script still does what I need depending on what number I set the int variable to. It just doesn't show in the inspector so I have to manually change the script.

Any ideas

???

edit..

Script is attached to a sceneManager object in my scene and accessed from another script using -

 LevelCleared.enemyNumbers -= 1;
 
 

???

Comment
Add comment · Show 7
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 digzou · Apr 08, 2015 at 07:24 AM 1
Share

Static variables dont show up in the inspector. You can assign it only as a public variable ans assign the public variable to another static variabe. Like,

public int enemyNumbers = 10; private static int staticEnemyNumber = enemyNumbers;

And then use staticEnemeyNumber throughout the script.

:)

avatar image Digital-Phantom · Apr 08, 2015 at 07:38 AM 0
Share

Assu$$anonymous$$g I understood you, my script should now look like -

 using UnityEngine;
 using System.Collections;
 
 public class LevelCleared : $$anonymous$$onoBehaviour
 {
 
     public int killCount = 10;
     private static int enemyNumbers = killCount;
     
 
     void Update()
     {
         if(enemyNumbers <= 0)
         {
             var i = Application.loadedLevel;
             AutoFade.LoadLevel(i+1 ,2,1,Color.black);
             //Parameter 1 - LevelName or LevelIndex
             //Parameter 2 - FadeOutTime - time in seconds until the level actually starts loading
             //Parameter 3 - FadeInTime - time in seconds until the fade-in process is completed.
             //Parameter 4 - FadeColor - this is the color the screen fades to.
         }
     }
 }
 
 

But I,m getting compiler error -

Assets/LevelCleared.cs(8,43): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `LevelCleared.killCount'

???

avatar image Nerevar · Apr 08, 2015 at 07:51 AM 0
Share

You should initialize your static variable in a start or awake function to be sure that killCount exists.

 void Awake(){
    enemyNumbers = killCount;
 
 }
avatar image Digital-Phantom · Apr 08, 2015 at 08:00 AM 0
Share
 using UnityEngine;
 using System.Collections;
 
 public class LevelCleared : $$anonymous$$onoBehaviour
 {
 
     public int killCount = 10;
     private static int enemyNumbers = killCount;
     
 
     void Awake()
     {
         enemyNumbers = killCount;
     }
 
 
     void Update()
     {
         if(enemyNumbers <= 0)
         {
             var i = Application.loadedLevel;
             AutoFade.LoadLevel(i+1 ,2,1,Color.black);
             //Parameter 1 - LevelName or LevelIndex
             //Parameter 2 - FadeOutTime - time in seconds until the level actually starts loading
             //Parameter 3 - FadeInTime - time in seconds until the fade-in process is completed.
             //Parameter 4 - FadeColor - this is the color the screen fades to.
         }
     }
 }

   
 

Gives error -

Assets/LevelCleared.cs(8,43): error CS0236: A field initializer cannot reference the nonstatic field, method, or property `LevelCleared.killCount'

avatar image Nerevar · Apr 08, 2015 at 08:14 AM 2
Share

well the error is not in this cript, put your static variable to public and that should do it.

Show more comments

1 Reply

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

Answer by Digital-Phantom · Apr 08, 2015 at 08:16 AM

ok problem solved (although I'll admit I don't understand why...lol)

 using UnityEngine;
 using System.Collections;
 
 public class LevelCleared : MonoBehaviour
 {
 
     public int killCount = 10;
     public static int enemyNumbers;
     
 
     void Awake()
     {
         enemyNumbers  = killCount;
     }
 
 
     void Update()
     {
         if(enemyNumbers <= 0)
         {
             var i = Application.loadedLevel;
             AutoFade.LoadLevel(i+1 ,2,1,Color.black);
             //Parameter 1 - LevelName or LevelIndex
             //Parameter 2 - FadeOutTime - time in seconds until the level actually starts loading
             //Parameter 3 - FadeInTime - time in seconds until the fade-in process is completed.
             //Parameter 4 - FadeColor - this is the color the screen fades to.
         }
     }
 }
 
 

Changing the enemyNumbers to a public static in (in conjunction with the killCount variable) works fine. I get to edit the value in the inspector and still access the script from other scripts.

Seems like a waste of time to me, I'm still able to influence a static variable from the inspector, just takes another few lines of code to do it. I mean... why not just make it possible to have a public static int in the first place.

Anyway, all sorted. Thanks for the help guys

:)

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 winsjansen · Apr 08, 2015 at 11:45 AM 0
Share

I think you misunderstand the meaning of static my friend, it has nothing to do with accessibility, other then that you can't edit a static member on the inspector because, well it's static.

You can't just access variables by setting them to public, you still need a reference to what instance of the class you want the value from.

The only reason you can access a static variable is because it's a static value that is the same for all instances of the class.

The way you are doing here is NOT correct, and WILL give you problems in the future.

avatar image ysittas winsjansen · Apr 21, 2019 at 09:06 AM 0
Share

@winsjansen Can you please elaborate on the correct way then?

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

22 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

Related Questions

Is it possible to show Static Variables in the Inspector? 10 Answers

How to set a public variable to a gameObject in the inspector through coding 1 Answer

Can I use strings with static variables? 2 Answers

Whats the difference between public and static variable? 2 Answers

Avoid public variable to get overwritten by the Inspector 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