Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Make a box in editor

Discussion in 'Scripting' started by megisto, Apr 23, 2014.

  1. megisto

    megisto

    Joined:
    Dec 25, 2009
    Posts:
    127
    Hi all, i am studying editor scripting, i am pretty new to this, i am following some of the documentation out there. Here is the question:

    Just to test i want to make a window with a button that when pressed calls a method from another script that build a box.

    First script is called MyWindow, and here is the code:

    Code (csharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. public class MyWindow : EditorWindow
    5. {
    6.     string myString = "Hello world";
    7.     bool groupEnabled;
    8.     bool myBool = true;
    9.     float myFloat = 1.23f;
    10.  
    11.     [MenuItem("Window/My Window")]
    12.     public static void ShowWindow()
    13.     {
    14.  
    15.         EditorWindow.GetWindow(typeof(MyWindow));
    16.     }
    17.  
    18.      void OnGUI()
    19.     {
    20.         GUILayout.Label ("Base Settings", EditorStyles.boldLabel);
    21.         myString = EditorGUILayout.TextField ("Text Field", myString);
    22.  
    23.         groupEnabled = EditorGUILayout.BeginToggleGroup("Optional Settings", groupEnabled);
    24.         myBool = EditorGUILayout.Toggle("Toggle", myBool);
    25.         myFloat = EditorGUILayout.Slider ("Slider", myFloat, -3, 3);
    26.         EditorGUILayout.EndToggleGroup();
    27.  
    28.         MakeABox myScript = (MakeABox)target;
    29.         if(GUILayout.Button ("Build Box"))
    30.         {
    31.             myScript.BuildBox();
    32.         }
    33.     }
    34. }
    Is it the script from the docs, plus some lines i wrote from "MakeABox etc."

    The second one is called MakeABox and it contains the following code:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MakeABox : MonoBehaviour {
    5.  
    6.     // Use this for initialization
    7.     public void BuildBox () {
    8.         GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    9.         cube.transform.position = new Vector3(0,0.5f,0);
    10.     }
    11.    
    12.     // Update is called once per frame
    13.     void Update () {
    14.    
    15.     }
    16. }
    I expected to see a button on my custom window and when i press it should appear a cube. But all i got is an error in the console, which says "Assets/Editor/MyWindow.cs(29,47): error CS0103: The name `target' does not exist in the current context", and no button at all.

    Scripts are both in a folder called "Editor".

    Can you help?
     
  2. marcupoilu

    marcupoilu

    Joined:
    Dec 3, 2012
    Posts:
    1
    Hi Megisto,

    target is a ref to the Class type you want to alter. So my guess is you did not defined the class type target should focus.

    Try to do something like this just before you enter your class :

    if you're using C# :
    [CustomEditor(typeof(YourClassTypeToModify), true)]

    if you're using JS :
    @CustomEditor(YourClassTypeToModify)

    then you can use target as you already do : MakeABox myScript = (MakeABox)target;
    Also you do not have to put both scripts in the Editor folder, just the customEditor one.

    Hope that helps !

    (sry for my english)

    Marcupoilu
     
  3. megisto

    megisto

    Joined:
    Dec 25, 2009
    Posts:
    127
    I am sorry, I still cannot this to work, i get the same message. Maybe i am putting this line in the wrong place.

    Can you please copy/paste my code with your correction in it?
     
  4. jschieck

    jschieck

    Joined:
    Nov 10, 2010
    Posts:
    429
    If you're not trying to make a custom inspector "target" is not going to be anything and that other post won't help you. You can either use EditorGUILayout.ObjectField and drag in the object you're trying to call the script on, or use Selection.activeGameObject and call GetCompoent<MakeABox>().

    If you want it to be a custom inspector you'd need to make it like...

    Code (csharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4. [CustomEditor(typeof(MakeABox ))]
    5. public class MyWindow : Editor
    6. {
    7. ...
    and instead of OnGUI use OnInspectorGUI, then inside that call base.OnInspectorGUI() and then anything additional you want to draw like a button to build the box.

    https://unity3d.com/learn/tutorials/modules/intermediate/editor/building-custom-inspector
     
  5. megisto

    megisto

    Joined:
    Dec 25, 2009
    Posts:
    127
    Got it, thank you! I was using the wrong logic... I was calling another script, and in that case i would use a Custom editor. Instead i wrote the function to create the cube inside the script in the ditor, and now it works.

    Here is the code:

    Code (csharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4.  
    5. public class MyWindow : EditorWindow
    6. {
    7.  
    8.     string myString = "Hello world";
    9.     bool groupEnabled;
    10.     bool myBool = true;
    11.     float myFloat = 1.23f;
    12.  
    13.     [MenuItem("Window/My Window")]
    14.  
    15.     public static void ShowWindow()
    16.     {
    17.         EditorWindow.GetWindow(typeof(MyWindow));
    18.     }
    19.  
    20.      void OnGUI()
    21.     {
    22.         GUILayout.Label ("Base Settings", EditorStyles.boldLabel);
    23.         myString = EditorGUILayout.TextField ("Text Field", myString);
    24.  
    25.         groupEnabled = EditorGUILayout.BeginToggleGroup("Optional Settings", groupEnabled);
    26.         myBool = EditorGUILayout.Toggle("Toggle", myBool);
    27.         myFloat = EditorGUILayout.Slider ("Slider", myFloat, -3, 3);
    28.         EditorGUILayout.EndToggleGroup();
    29.  
    30.             if(GUILayout.Button ("Build Box"))
    31.         {
    32.             GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
    33.             cube.transform.position = new Vector3(0,0,0);
    34.         }
    35.     }
    36. }
    Thank you