- I created Test.cs and TestEditor.cs (Test class is a simple class.)
- Test.cs is empty and here is TestEditor.cs:
using UnityEngine; using UnityEditor; [CustomEditor(typeof(Test))] public class TestEditor : Editor { Test test; GameObject cube; GameObject sphere; public override void OnInspectorGUI() { base.OnInspectorGUI(); test = (Test)target; if (GUILayout.Button("Create")) { Create(); RayCastTest(); } if (GUILayout.Button("RayCast Test")) { RayCastTest(); } } void Create() { cube = GameObject.CreatePrimitive(PrimitiveType.Cube); cube.transform.position = test.transform.position; sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere); sphere.transform.position = test.transform.position + Vector3.right * 5f; } void RayCastTest() { Ray ray; RaycastHit hit; ray = new Ray(test.transform.position, Vector3.right); if (Physics.Raycast(ray, out hit, 1000f)) { GameObject target = hit.collider.gameObject; target.transform.localScale *= 2f; } } }
- If I press the "Create" button in Unity, the objects are created but the RayCastTest() function does not work. But, if I press the "Create" button first and then the "RayCast Test" button, the function works.
So, How can I get the RayCastTest() function to work in the first part?