- Home /
Better way to call function from another script from editor script?
I know this has been asked a dozen times, but I'm going mad...
Is there a better way to call another scripts function from my editor script? This just feels incredibly duct taped to me. I've even tried moving the entire function I'm trying to call over to this script, but because this is a Editor script, not MonoBehavior, I can't use the right variables and methods. This script just makes a little button in the inspector that I can press.
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomEditor(typeof(NavPointManager))]
public class NavPointEditor : Editor
{
GameObject obj;
public override void OnInspectorGUI()
{
serializedObject.Update();
if (GUILayout.Button("Generate NavPoints", GUILayout.Width(150)))
{
obj = GameObject.Find("HexGrid");
obj.SendMessage("GenerateNavPoints");
}
}
}
Answer by Berenger · Jun 03, 2012 at 05:10 PM
You should check if obj is found before using it. Aside from that, the search by name is the slowest one. By tag or type is better, but it's not really a big deal if you have to wait in the editor.
Yeah, was thinking that because it was in the editor it didn't matter anyway. I'm getting an error though... seen this?
!IsPlayingOrAllowExecuteInEdit$$anonymous$$ode () UnityEngine.GameObject:Send$$anonymous$$essage(String) NavPointEditor:OnInspectorGUI() (at Assets/Editor/NavPointEditor.cs:22) UnityEditor.DockArea:OnGUI()
$$anonymous$$eh, just added [ExecuteInEdit$$anonymous$$ode] on top and it went away. Good stuff.
Your answer
Follow this Question
Related Questions
Access a function from one script in another? 3 Answers
"transform" in a static function 1 Answer
Generic way to get properties on Component? 3 Answers
Script to add script to all children 1 Answer
Perform action on save/load in editor 2 Answers