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): using UnityEditor; using UnityEngine; public class MyWindow : EditorWindow { string myString = "Hello world"; bool groupEnabled; bool myBool = true; float myFloat = 1.23f; [MenuItem("Window/My Window")] public static void ShowWindow() { EditorWindow.GetWindow(typeof(MyWindow)); } void OnGUI() { GUILayout.Label ("Base Settings", EditorStyles.boldLabel); myString = EditorGUILayout.TextField ("Text Field", myString); groupEnabled = EditorGUILayout.BeginToggleGroup("Optional Settings", groupEnabled); myBool = EditorGUILayout.Toggle("Toggle", myBool); myFloat = EditorGUILayout.Slider ("Slider", myFloat, -3, 3); EditorGUILayout.EndToggleGroup(); MakeABox myScript = (MakeABox)target; if(GUILayout.Button ("Build Box")) { myScript.BuildBox(); } } } 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): using UnityEngine; using System.Collections; public class MakeABox : MonoBehaviour { // Use this for initialization public void BuildBox () { GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.transform.position = new Vector3(0,0.5f,0); } // Update is called once per frame void Update () { } } 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?
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
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?
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): using UnityEditor; using UnityEngine; [CustomEditor(typeof(MakeABox ))] public class MyWindow : Editor { ... 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
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): using UnityEditor; using UnityEngine; public class MyWindow : EditorWindow { string myString = "Hello world"; bool groupEnabled; bool myBool = true; float myFloat = 1.23f; [MenuItem("Window/My Window")] public static void ShowWindow() { EditorWindow.GetWindow(typeof(MyWindow)); } void OnGUI() { GUILayout.Label ("Base Settings", EditorStyles.boldLabel); myString = EditorGUILayout.TextField ("Text Field", myString); groupEnabled = EditorGUILayout.BeginToggleGroup("Optional Settings", groupEnabled); myBool = EditorGUILayout.Toggle("Toggle", myBool); myFloat = EditorGUILayout.Slider ("Slider", myFloat, -3, 3); EditorGUILayout.EndToggleGroup(); if(GUILayout.Button ("Build Box")) { GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.transform.position = new Vector3(0,0,0); } } } Thank you