- Home /
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);
}
}
}
$$anonymous$$onoBehaviours don't need to have the Serializable
attribute
Have you attached the components on gameObjects?
Do you have any error in the console? Serialization can't happen if there are compiler errors
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
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
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.
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.
But in the asset Hexsphere there they are using a List of a class created by themself and that list does show up
Sorry, that's the best I can do. It's more of an editor-specific question than a coding question.
Answer by exploringunity · Jul 23, 2020 at 08:52 PM
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<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!