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 stadlernicolas26 · Jul 22, 2020 at 02:30 PM · inspectorserializationpublic variableserializable

Public fields not showing in inspector

My problem is that none of my public variables are not shown in the inspector, even though i have the [Serializable] at the beggining of all classes and every thread I read did not sole it. What is my mistake?

Code from my PlanetData.cs:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.Dynamic;
 using System;
 
 [Serializable]
 public class PlanetData : MonoBehaviour
 {
 
     public List<dynamic> islands = new List<dynamic>();
     public int islandCount = 4;
 
     // Start is called before the first frame update
     void Start()
     {
         for (int i = 0; i < islandCount; i++)
         {
             islands.Add(new ExpandoObject());
             islands[i].persons = new List<Person>();
             islands[i].houses = new List<House>();
         }
 
         for (int i = 0; i < 50; i++)
         {
             GameObject personObj = new GameObject("PersonGameObject");
             personObj.transform.SetParent(GameObject.Find("All Objects/island1/Persons").transform);
             personObj.AddComponent<Person>();
             //GameObject houseObj = new GameObject("House");
             //houseObj.transform.SetParent(GameObject.Find("All Objects/island1/Houses").transform);
             //houseObj.AddComponent<House>();
             islands[0].persons.Add(personObj.GetComponent<Person>());
             //islands[0].houses.Add(houseObj.GetComponent<House>());
         }
 
         for (int i = 0; i < 4; i++)
         {
             GameObject h = Instantiate(GameObject.Find("HouseGameObject"));
             this.gameObject.GetComponent<Hexsphere>().tiles[i].placeObject(h);
             islands[0].houses.Add(h.GetComponent<House>());
         }
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 }

Person.cs:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System;
 
 [Serializable]
 public class Person : MonoBehaviour 
 {
   
   public string personName = "";
   public House house;
   public bool hasHouse = false;
 
   void Start() {
     setName();
   }
 
   void Update() {
     if (!hasHouse) {
       findHouse();
     }
     if (personName == "") {
       setName();
     }
   }
 
   public void remakePerson() {
     findHouse();
     setName();
   }
 
   public void findHouse() {
     GameObject houses = transform.parent.parent.Find("Houses").gameObject;
     for (int i = 0; i < houses.transform.childCount; i++)
     {
       House h = houses.transform.GetChild(i).GetComponent<House>();
       if (h.isSpotFree()) {
         h.addPerson(this);
         Debug.Log("found House");
         house = h;
         break;
       }
     }
 
     hasHouse = true;
   }
 
   public void setName() {
     int rnd = Mathf.RoundToInt(UnityEngine.Random.Range(0f, 10f));
     switch (rnd)
     {
       case 0:
         personName = "Elisabeth";
         break;
       case 1:
         personName = "Susanne";
         break;
       case 2:
         personName = "Simone";
         break;
       case 3:
         personName = "Katharina";
         break;
       case 4:
         personName = "Anna";
         break;
       case 5:
         personName = "Johannes";
         break;
       case 6:
         personName = "Karl";
         break;
       case 7:
         personName = "Friedrich";
         break;
       case 8:
         personName = "Hubert";
         break;
       case 9:
         personName = "Maximilian";
         break;
       case 10:
         personName = "Peter";
         break;
     }
   }
 
 }


House.cs:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System;
 
 [Serializable]
 public class House : MonoBehaviour
 {
 
   public int lvl = 0;
   public int maxPersons = 4;
   public List<Person> persons = new List<Person>();
 
 
   public bool isSpotFree() {
     if (persons.Count < maxPersons) {
       return true;
     }
     return false;
 
   }
 
   public void addPerson(Person person) {
     if (!isSpotFree()) {
       Debug.Log("The house is full");
       return;
     } else {
       persons.Add(person);
     }
 
   }
   
 }




Comment
Add comment · Show 13
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 Hellium · Jul 22, 2020 at 02:34 PM 0
Share

$$anonymous$$onoBehaviours don't need to have the Serializable attribute


  1. Have you attached the components on gameObjects?

  2. Do you have any error in the console? Serialization can't happen if there are compiler errors

avatar image stadlernicolas26 Hellium · Jul 22, 2020 at 02:37 PM 0
Share

Well I have the script attached to a sphere and there are no errors, I am able to run my game. I also tried without Serializable but the List dynamic is not showing

avatar image Hellium stadlernicolas26 · Jul 22, 2020 at 02:41 PM 1
Share

Oh, so it's only this list? Then, yes, it's normal.


https://docs.unity3d.com/$$anonymous$$anual/script-Serialization.html


Simple field types that can be serialized

  • References to objects that derive from UnityEngine.Object

  • Primitive data types (int, float, double, bool, string, etc.)

    • Enum types

    • Certain Unity built-in types: Vector2, Vector3, Vector4, Rect, Quaternion, $$anonymous$$atrix4x4, Color, Color32, Layer$$anonymous$$ask, AnimationCurve, Gradient, RectOffset, GUIStyle

Show more comments
avatar image stadlernicolas26 Hellium · Jul 22, 2020 at 02:40 PM 0
Share

The reason why i used Serializable was that the List didnt show up and I remembered that in my last project i solved that problem by doing anything with the serialization,but i cant remember exactly what i did.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Weaver13 · Jul 23, 2020 at 05:21 AM

I'm going out on a limb here and assuming that SOME variables do show in the inspector, for instance

 int islandCount;
 public string personName;

These are things that can be manipulated through the inspector because they are either simple data types or Unity Editor supported. However many of your variables are classes that you created and Unity therefore has no idea of how to handle manipulating them in the inspector so it hides them. So it won't show:

 public House myHouse;

And that makes sense because you can't manipulate it in the editor. As for if you're wondering if you can edit the fields/properties of a class, consider that if you write:

 public Rigidbody rb;

It doesn't show the Rigidbody class fields for you to mess with because the inspector is just not that complex. The same goes for House. If it's that important, what you could do is this:

 MyMonoBehaviourScript : MonoBehaviour {
     public string houseData;
     public House myHouse;
     void Update(){
         myHouse = House.Parse(houseData);
     }
 }

And:

 public class House{
     public static House Parse(string input){
         House h = new House();
         string[] fields = input.Split(';');
         foreach f in fields: //yes this is pseudo
             string[] KeyValues = f.Split(':');
             foreach k in keyValues:
                 if (k[0] == "myField1") h.myField1 = float.Parse(k[1]);
         return h;
     }
 }

This would help with the visualizing. Alternatively, houseData could be array instead of string, and you could use a switch operator if you have a lot of fields to parse. Then create a similar parsing method for the Person class so that you can also see them, but I do not recommend using this code in a final build. I don't think what you're trying is possible without a messy hack.

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 stadlernicolas26 · Jul 23, 2020 at 08:32 AM 0
Share

But in the asset Hexsphere there they are using a List of a class created by themself and that list does show up

avatar image Weaver13 stadlernicolas26 · Jul 23, 2020 at 07:41 PM 0
Share

Sorry, that's the best I can do. It's more of an editor-specific question than a coding question.

avatar image
0

Answer by exploringunity · Jul 23, 2020 at 08:52 PM

@stadlernicolas26,

Unity doesn't appear to support the dynamic type in the Inspector. Simple example:

 using System.Collections.Generic;
 using UnityEngine;
 
 public class DynamicTest : MonoBehaviour
 {
     public int myInt;
     public List<int> myInts;
     public dynamic myDynamic;
     public List<dynamic> myDynamics;
 }

After putting this component on a GameObject, you get this in the Inspector:

The int and list of ints display, but not the dynamic or list of dynamics

The int and List<int> show up fine, but the fields that have dynamic do not.

In general, you should avoid using dynamic / ExpandoObject. It's very rare that you can't achieve whatever you're trying to do with a custom class or often just a Dictionary.

In your case, you could try making something simple like this:

 [System.Serializable]
 public class IslandData {
     public List<Person> persons;
     public List<House> houses;
 }

Using this instead of the dynamic/ExpandoObject should work with the Inspector. Hope this helps!


screenshot-13.png (7.5 kB)
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

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

144 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 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

Help with editor serialization 1 Answer

Serializable class with access to its Monobehaviour host 2 Answers

Error when trying to Serialize a field that is in a class 0 Answers

How do I save scripts as variables in the inspector? 0 Answers

Saving editor-only variables 0 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