0
\$\begingroup\$

I am new to Unity. I have a simple game object with one script attached to it and then another script to add a button which will call the function within the script attached to the game object. My problem is I want to have two editable fields height and width but if the custom editor is added to the game object then those public fields no longer display in the inspector window...Not sure why?

WorldGenerator.cs:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WorldGenerator : MonoBehaviour {

public int height;
public int width;

public void RunWorldGeneration()
{
    Generate worldGenerator = new Generate();
    float[,] world = worldGenerator.GenerateWorld(width, height);

}
}

WorldGeneratorEditor.cs:

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

[CustomEditor(typeof(WorldGenerator))] // If I comment this out the public variablbes show
// If it is uncommented, then just the button with "Generate" text shows
public class MapGeneratorEditor : Editor
{

public override void OnInspectorGUI()
{
    WorldGenerator worldGen = (WorldGenerator)target;

    if (GUILayout.Button("Generate"))
    {
        worldGen.RunWorldGeneration();
    };
}
}

When the custom editor is active

When custom editor is inactive

\$\endgroup\$
1
  • \$\begingroup\$ There are no errors, by the way. At least none in the console. \$\endgroup\$
    – Alex
    Apr 24, 2017 at 4:26

2 Answers 2

2
\$\begingroup\$

From the docs on Editor.DrawDefaultInspector():

Description

Draw the built-in inspector.

Call this function from inside OnInspectorGUI method to draw the automatic inspector. It is useful you don't want to redo the entire inspector, but you want to add a few buttons to it.

If you don't include a call to this method, Unity will assume your custom inspector is drawing everything it wants drawn — that way you can draw fields in customised ways without them showing up twice, or hide serialised fields you don't want to edit directly.

\$\endgroup\$
1
\$\begingroup\$

It's usually best practise to add base.OnInspectorGUI(); at the top of the OnInspectorGUI() method.

public override void OnInspectorGUI()
{
    base.OnInspectorGUI();

    WorldGenerator worldGen = (WorldGenerator)target;

    if (GUILayout.Button("Generate"))
        worldGen.RunWorldGeneration();
}
```
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .