- Home /
Some Public Variables doesn't display in Inspector
Just like below, this is my script, it's very strange that the public variables can't all display in the inspector. It just displays "myScriptableObject" in the inspector but other public variables no matter what type and which it's serializable. How can I solve this problem?
using UnityEngine;
using System;
using System.Collections;
using System.Collections.Generic;
public class AttributesControl : MonoBehaviour
{
public MyScriptableObjects myScriptableObject;
public float randomMin;
public float randomMax;
public float attrMin;
public float attrMax;
}
$$anonymous$$y ScriptableObject just like this
using UnityEngine;
using System.Collections;
using System;
using System.Collections.Generic;
[Serializable]
public class $$anonymous$$aterialsObject : ScriptableObject {
[Serializable]
public class $$anonymous$$aterial : ScriptableObject
{
public int materialId;
public string materialName;
public int materialRarilty;
}
public List<$$anonymous$$aterial> materialList;
}
Is this coding correct?
That's not the definition of $$anonymous$$yScriptableObjects - that's the definition of $$anonymous$$aterialsObject...
I know, I just change it here they're exactly correct in my project and I have built my project with no error sure I haven't use $$anonymous$$aterialsObject at all and the $$anonymous$$yScriptableObject is exactly the same as $$anonymous$$aterialsObject. And now the inspector can display the public instance but no public variables.
Answer by Bunny83 · Dec 30, 2015 at 12:17 PM
The ScriptableObject class is used to create seperate assets which aren't gameobjects. If you declare a variable to a ScriptableObject you can only assign a ScriptableObject asset to that variable. Just like any other asset reference. ScriptableObjects have to be created explicitly in the editor and they can be edited seperately. Also ScriptableObjects have a similar restriction as MonoBehaviour classes. They also need to be placed in their own script file and have their filenames match the classname.
To me it looks like you actually don't want to use ScriptableObjects here. Just use serializable classes:
[Serializable]
public class MaterialsObject
{
[Serializable]
public class Material
{
public int materialId;
public string materialName;
public int materialRarilty;
}
public List<Material> materialList;
}
While it's possible to call your own class "Material" it will just cause problems with Unitys Material class.
Understood, but why I can't see any variables in the inspector? Looks like I removed all scripts and scriptableobjects then make a new script only includes one line code "public int test" and the default codes, it neither displays public variables at all.......I have no idea why this happens.......PS: I found something just now and looked like it will be useful, anyway, thanks a lot, man
As i said, ScriptableObjects are standalone assets. You will only see a reference field when you create a variable of your ScriptableObject type. Unity also doesn't create an instance automatically for you like it does for "normal" serializable classes. You don't see the variables just like you don't see the variables of a different $$anonymous$$onoBehaviour script. Example:
public class SomeClass : $$anonymous$$onoBehaviour
{
public int someInt;
public string someText;
}
public class Test : $$anonymous$$onoBehaviour
{
public SomeClass other;
}
Here when you attach the Test class to a gameobject, you won't see the "someInt" and someText variables in the Test class. You only see a reference field called "other" where you can link an instance of the "SomeClass" class. The variables of that instance can only be viewed, when you inspect the SomeClass instance directly. ScriptableObjects work exactly the same way, but they are not components attached to gameobjects. They are used to store information as a seperate asset file in your project. When you select such an asset, you can view and edit it's variables.
Unitys TerrainData class is the only classic example of a ScriptableObject in use. Though its serialized variables are hidden on purpose since you shouldn't edit that data directly.
Here's a great introduction to ScriptableObjects but it's a bit lengthy.
As i said ScriptableObjects are mainly used for storing data as assets. Unity has even added the CreateAsset$$anonymous$$enuAttribute to simplify the creation of an asset. ScriptableObjects always have to be created explicitly
Thank you for your answer, easy to me to understand. I have found out where the problem is. Anyway, thanks!
Answer by rajan4uto · Dec 30, 2015 at 09:34 AM
I think there are error in other script on same project. Just check it once. If yes then correct it then after you can see in inspector.