Passing value from Custom Editor Script to a mono behaviour script

I’ve created a custom inspector window in Unity where I have a bunch of text fields where I can input values. With these text fields I’m passing them into a method that passes them as strings inside that method.

It looks something like this:

Editor Window code

	string event_NameString = "";
	string event_ActionString = "";
	string event_Label = "";

	public override void OnInspectorGUI()
	{
		GUILayout.BeginVertical();

		GUILayout.Space(10);
		enum_Index = EditorGUILayout.Popup(enum_Index, enum_Option);
	
		event_NameString = EditorGUILayout.TextField("Event Category", event_NameString);
		event_ActionString = EditorGUILayout.TextField("Event Action", event_ActionString);
		event_Label = EditorGUILayout.TextField("Event Label", event_Label);

		GUILayout.Space(5);

		GUILayout.EndVertical();
	}

My method that I’m wanting to pass each of those three strings into in another class:

     public static void buttonHit(string eventName, string eventAction, string description)
     {
    	gua.sendEventHit(eventName,eventAction,description,1);
     }

Then finally, in my third class where I want to call that method:

	void OnGUI()
	{
		if(GUI.Button(new Rect(0,0,100,100), "Send stuff"))
			Analytics.gua.sendEventHit(Test, Test2, Test3, 1);
	}

As my first class with the editor window code is inheriting the Editor class, and my third inheriting the MonoBehaviour class I have no way of passing the values contained in event_NameString to my method.

Could someone please tell me how I can get the inputted values in the editor window and have them passed to my in game object?

In “Editor”, there’s a property called “target”.

Target is the object that is currently being inspected. In your case, that would be the instance of your MonoBehaviour.

1 Like