• 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 SymphonicYT · Feb 12, 2019 at 12:26 AM · inspector

Unity refuses to display my field in the Inspector.

It wont display the playerList text field.

 using UnityEngine;
 using System.Collections.Generic;
 using System.Collections;
 using UnityEngine.UI;
 
 public class GameManager : MonoBehaviour
 {
     
     private const string PLAYER_ID_PREFIX = "Player ";
     
  
     private static Dictionary<string, Player> players = new Dictionary<string, Player>(); // freaking tutorial doesnt say to use void start 
 
     //[SerializeField]
     public static Text playerList;
  
  public static void RegisterPlayer (string _netID, Player _player) {
      
      string _playerID = PLAYER_ID_PREFIX + _netID;
      players.Add(_playerID, _player); // Player 1, 0af68923r
      _player.transform.name = _playerID;
      DisplayUI();
 
 
  }
  
  public static void UnRegisterPlayer (string _playerID) {
      
      players.Remove(_playerID);
      
  }
  
  public static Player GetPlayer(string _playerID) {
      
      return players[_playerID];
      
  }
  
 public static void DisplayUI()
     {
 
         foreach (string _playerID in players.Keys)
         {
 
             playerList.text = playerList.text + _playerID + " - " + players[_playerID].transform.name + "/n";
 
         }
 
     }
  
 }
 
Comment
Add comment
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 Reply

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

Answer by Bunny83 · Feb 12, 2019 at 12:44 AM

Because you declared your field static. Static members do not belong to an instance of that class. They are seperate, static fields which are independent from instances of the class and are shared between all of them. Static fields are never serialized when an instance of a class is serialized since, as i said, it doesn't even belong to the instance.


If you want a field to be serialized (and therefore be displayed and editable in the inspector) it need to be a non static instance field.


It's difficult to give you a clear advice what you should change in your class since we don't know where and how it's used. At the moment your class has no instance fields or methods. So attaching it to a gameobject is completely pointless at the moment. However for managers that should be represented by an instance you usually want to use a "singleton". A singletun is just a single instance of an object and you provide a static field which holds a reference to that single instance.

 public class GameManager : MonoBehaviour
 {
     public static GameManager instance;
     void Awake()
     {
         instance = this;
     }
 
     public Text playerList;
     // [ ... ]
 }


When the game starts the instance of the GameManager in the scene will "store itself" in the static instance variable. Now everybody, anywhere in your project can use GameManager.instance to get access to the instance of the class and therefore has access to all instance fields and methods,

Comment
Add comment · Show 3 · 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 SymphonicYT · Feb 12, 2019 at 02:18 AM 0
Share

Thanks a lot, im going to ramble now to see if i understand all this static instance lingo and stuff.

So basically a function marked static is not really "belonging" to any script, and it cant use variables from the script it is in. Once you make a "singleton" its like referring to the main script from the static function (like another script almost even though its in the same script), so then inside of a static function i can use this variable now by calling on the instance thing.

and static things arent serializble because they "arent" part of the same script, in a way. right?

oh and one static thing can do stuff in other static things without a reference right?

so confused :/

avatar image Bunny83 SymphonicYT · Feb 12, 2019 at 04:38 AM 0
Share

Yes, that was a pretty accurate summarization :)


What even experienced programmers often don't realise or understand is that actually all methods (essentially all executable code) are always static behind the scenes. It's just syntactic sugar of our high level languages which give the impression of a method being "part" of an object instance. What actually happens is that instance methods have an additional implicit parameter which you don't see. So the actual method code only exists once in memory and is used by all the different instances.


Imagine a class like this

 public class $$anonymous$$yClass
 {
     public string strVar;
     public void DoSomething(string aText)
     {
         strVar = aText;
     }
 }

You would execute the method like this:

 $$anonymous$$yClass obj = new $$anonymous$$yClass();
 obj.DoSomething("Hello World");

However what actually happens behind the scenes is essentially this:

 public class $$anonymous$$yClass
 {
     public string strVar;
     public static void DoSomething($$anonymous$$yClass this, string aText)
     {
         this.strVar = aText;
     }
 }

And when executing the method the compiler actually does this:

 $$anonymous$$yClass obj = new $$anonymous$$yClass();
 $$anonymous$$yClass.DoSomething(obj, "Hello World");


Though since we strictly seperate "instance" members from static members the compiler doesn't allow you to access instance members without having an instance. It should be obvious that this is of course impossible for fields / variables since they are literally stored in the memory of the instance. However even instance methods can't be called without instance since that first implicit parameter need to have a reference to an instance.


Hopefully this doesn't confuse you even more ;) Though usually it helps to know a bit more about the background to better understand the top layer

avatar image SymphonicYT Bunny83 · Feb 12, 2019 at 01:20 PM 0
Share

I think i get it.... but I wont think too hard about or ill be stuck in a confusion loop again.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

101 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

DropDown Menu In Inspector? 1 Answer

How do I make a public array of arrays appear in the inspector? 1 Answer

Hide/Show properties dynamically in inspector. 6 Answers

Editor GUI: Object preview icon 2 Answers

Multidimentional Array for Inventory style system in 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