- Home /
Get variables from custom editor to script
How Do i get the variables from the customEditor to my script.
// The Editor Class: using UnityEngine; using UnityEditor;
[CustomEditor(typeof(Weapon))] public class MyWindow : Editor { string myString = "Hello World"; bool groupEnabled; bool myBool = true; float myFloat = 1.23f;
public override void OnInspectorGUI()
{
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 ();
}
// The Class the using the editor. This script says that the variables dosent exist
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Weapon : MonoBehaviour {
void Start()
{
Debug.Log(myString );
}
}
Answer by loramaru · May 07, 2011 at 06:29 PM
The problem is you are changing seetings on your editor itself rather than the object you want to edit. You need to move all the properties over to Weapon and make them public. Then use the target property of the editor to get at the instance of the Weapon you are editing.
public class Weapon : MonoBehaviour { public string myString = "Hello World"; public bool groupEnabled; public bool myBool = true; public float myFloat = 1.23f;
void Start()
{
Debug.Log(myString );
}
}
[CustomEditor(typeof(Weapon))] public class MyWindow : Editor { public override void OnInspectorGUI() { Weapon weapon = (Weapon)target;
GUILayout.Label ("Base Settings", EditorStyles.boldLabel);
weapon.myString = EditorGUILayout.TextField ("Text Field", weapon.myString);
weapon.groupEnabled = EditorGUILayout.BeginToggleGroup ("Optional Settings", weapon.groupEnabled);
weapon.myBool = EditorGUILayout.Toggle ("Toggle", weapon.myBool);
weapon.myFloat = EditorGUILayout.Slider ("Slider", weapon.myFloat, -3, 3);
EditorGUILayout.EndToggleGroup ();
}
}