Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Editor Script Default Values Not Showing In Inspector

Discussion in 'Scripting' started by Froggles01, May 24, 2021.

  1. Froggles01

    Froggles01

    Joined:
    Apr 2, 2021
    Posts:
    8
    This may sound like a stupid question, but I have an editor script for a window, and I want to make it spawn a baseplate, but the default variable for my baseplate prefab is not showing in the script in the inspector.

    Here is my script:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. public class SpawnerWindow : EditorWindow
    5. {
    6.     public enum ThingsToSpawn
    7.     {
    8.         Baseplate
    9.     }
    10.  
    11.     public ThingsToSpawn ItemOnScreen = ThingsToSpawn.Baseplate;
    12.  
    13.     #region Baseplate Variables
    14.     public string baseplateTooltip = "Spawns a baseplate with a given position and size to for you to add to your game.Comes with a basic material which can be changed.";
    15.  
    16.     private Color baseplateMaterialColour = new Color(53,53,53);
    17.  
    18.     private Vector3 baseplatePosition = Vector3.zero, baseplateSize = new Vector3(100,1,100);
    19.  
    20.     public GameObject baseplatePrefab;
    21.  
    22.     #endregion
    23.  
    24.     [MenuItem("Froggy's Utilities/Spawn Items")]
    25.     public static void OpenWindow()
    26.     {
    27.         GetWindow<SpawnerWindow>("Spawner Window");
    28.     }
    29.  
    30.     private void OnGUI()
    31.     {
    32.         GUILayout.Label("Spawn Useful Items Into Your Scene", EditorStyles.boldLabel);
    33.  
    34.         GUILayout.Space(10f);
    35.  
    36.         Debug.Log("OnGUI() was called");
    37.  
    38.         if(ItemOnScreen == ThingsToSpawn.Baseplate)//Checks if the baseplate window should be opened
    39.         {
    40.             Baseplate();
    41.         }
    42.     }
    43.  
    44.     #region Baseplate Code
    45.     private void Baseplate()
    46.     {
    47.         GUILayout.Box(new GUIContent("Spawn Baseplate", baseplateTooltip));
    48.  
    49.         baseplateMaterialColour = EditorGUILayout.ColorField("Material Colour", baseplateMaterialColour);
    50.  
    51.         baseplatePosition = EditorGUILayout.Vector3Field("Baseplate Position", baseplatePosition);
    52.         baseplateSize = EditorGUILayout.Vector3Field("Baseplate Size", baseplateSize);
    53.  
    54.         if (GUILayout.Button("Spawn"))
    55.         {
    56.             GameObject clone = Instantiate(baseplatePrefab, baseplatePosition, Quaternion.identity);
    57.             clone.transform.localScale = baseplateSize;
    58.  
    59.             Renderer cloneRenderer;
    60.             if (clone.TryGetComponent(out cloneRenderer))
    61.             {
    62.                 cloneRenderer.sharedMaterial.color = baseplateMaterialColour;
    63.             }
    64.         }
    65.     }
    66.     #endregion
    67. }
    68.  
     

    Attached Files:

  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi @Froggles01

    "I want to make it spawn a baseplate, but the default variable for my baseplate prefab is not showing in the script in the inspector."


    How do you actually populate your baseplatePrefab field? If you don't do that your field will be null.

    Also, Editor Window doesn't behave like MonoBehaviour's Inspector. It doesn't simply show you your public / serializable fields. You'll have to add a GUI element for anything you want to draw. In this case, you could add an Object Field, where you can drop a prefab from asset folder (for example, one way to do this). Then you'll have to catch that object, and it will be stored in your scripts field until you close your editor window.

    Something like this should work:
    Code (CSharp):
    1. private void OnGUI()
    2. {
    3.     // Heading
    4.     GUILayout.Label("Spawn Useful Items Into Your Scene", EditorStyles.boldLabel);
    5.  
    6.     // A Object Field to populate your baseplatePrefab
    7.     baseplatePrefab = EditorGUILayout.ObjectField(baseplatePrefab, typeof(GameObject), allowSceneObjects: false) as GameObject;
    8.  
    9.     GUILayout.Space(10f);
    10.  
    11.     if (ItemOnScreen == ThingsToSpawn.Baseplate)
    12.         Baseplate();
    13. }
    14.  
     
    Froggles01 likes this.
  3. Froggles01

    Froggles01

    Joined:
    Apr 2, 2021
    Posts:
    8
    Thank you, that helps a lot!