himanshu patel
himanshu patel

Reputation: 3

private/public Field is not seen in inspector when I use GridLayoutGroup in unity

private/public Field is not seen in inspector when i using GridLayoutGroup

here is the example

enter image description here

This is how I am defining my variables:

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine.UI; 
using UnityEngine; 
using UnityEditor; 

public class Myscript : GridLayoutGroup 
{ 
    [SerializeField] private bool ishhh; 
}

Upvotes: 0

Views: 1114

Answers (1)

derHugo
derHugo

Reputation: 90724

Most Unity built-in components have an according [CustomEditor] overwriting the default Inspector.

In specific the GridLayoutGroup has a custom inspector called GridLayoutGroupEditor overwriting the inspector for any class derived from GridLayoutGroup.

→ You have to inherit from it in order to make a custom editor for a class derived from GridLayoutGroup.

In order to make additional fields visible you could do something like e.g.

using UnityEditor;

...

[CustomEditor(typeof(Myscript))]
public class MyScriptEditor : GridLayoutGroupEditor
{
    private SerializedProperty ishhh;

    // Called when the Inspector is loaded
    // Usually when the according GameObject gets selected in the hierarchy
    private void OnEnable ()
    {
        // "Link" the SerializedProperty to a real serialized field
        // (public fields are serialized automatically)
        ishhh = serializedObject.FindProperty("ishhh");
    }

    // Kind of the Inspector's Update method
    public override void OnInpectorGUI ()
    {
        // Draw the default inspector
        // base.OnInspectorGUI();

        // Fetch current values into the serialized properties
        serializedObject.Update();

        // Automatically uses the correct field drawer according to the property type
        EditorGUILayout.PropertyField(ishhh);

        // Write back changed values to the real component
        serializedObject.ApplyModifiedProperties();
    }
}

Important: Place this script in a folder called Editor so it is stripped of in a build to avoid built-errors regarding the UnityEditor namespace.

Alternatively (since I can see that you already have a using UnityEditor in your example code) you can leave it in the same script but then you have to manually use #if Pre-Processors in order to strip all code-blocks/-lines off that use something from the UnityEditor namespace like

#if UNITY_EDITOR

    using UnityEditor;

#endif

...

#if UNITY_EDITOR

    // any code using UnityEditor

#endif

Otherwise you won't be able to build your app since the UnityEditor only exists in the Unity Editor itself and is completely stripped off for builds.


Note: Typed on smartphone so no warranty but I hope the idea gets clear

Upvotes: 2

Related Questions