Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Trouble Understanding Basic Editor Scripts (Please Help!)

Discussion in 'Scripting' started by orionburcham, Jun 27, 2011.

  1. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    491
    I'm working through Unity's Editor Scripting beginner tutorial, but for the life of me I can't grasp how 'target' is meant to be used. I'm also trying to translate on the fly from JS to C#, which is hard since I don't know any JS :p

    If anyone can spare a free moment to translate this JS example into C#, it would save me many tears and headbashing:

    first in JS:
    Code (csharp):
    1.  
    2.  
    3. @CustomEditor (LookAtPoint)
    4. class LookAtPointEditor extends Editor {
    5.     function OnInspectorGUI () {
    6.         [B]// I'm missing the following line: //[/B]
    7.         [B]target.lookAtPoint = EditorGUILayout.Vector3Field ("Look At Point", target.lookAtPoint);[/B]
    8.         if (GUI.changed)
    9.             EditorUtility.SetDirty (target);
    10.     }
    11. }
    12.  
    13.  

    and here's what I've got so far in C# (but it's incomplete, as I don't understand how to use 'target')

    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using UnityEditor;
    5. using System.Collections;
    6.  
    7. [CustomEditor(typeof(LookAtPoint))]
    8. public class LookAtPointEditor : Editor
    9. {
    10.     public override void OnInspectorGUI ()
    11.     {  
    12.                 [B]// missing line //[/B]
    13.                 if (GUI.changed)
    14.         {
    15.                         EditorUtility.SetDirty (target);
    16.         }
    17.         }
    18. }
    19.  
    20.  
    21.  
    Muchas Gracias!
    -Orion
     
    Last edited: Jun 27, 2011
  2. celadon

    celadon

    Joined:
    Oct 24, 2010
    Posts:
    16
    target is the selected object that is being edited by the Editor script. So, in your case, it would be the LookAtPoint component on the selected GameObject. I think the line you have highlighted would probably be the same in JS and C#... it is just defining a Vector3Field widget to get a new value for the lookAtPoint variable in the script component.
     
  3. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    491
    Thanks for the help. Still having trouble- Could you take a look?:

    So I copied that line into my script. The resulting c# code:

    Code (csharp):
    1.  
    2.  
    3. using UnityEngine;
    4. using UnityEditor;
    5. using System.Collections;
    6.  
    7. [CustomEditor(typeof(LookAtPoint))]
    8. public class LookAtPointEditor : Editor
    9. {
    10.     public override void OnInspectorGUI ()
    11.     {
    12.                  target.lookAtPoint = EditorGUILayout.Vector3Field ("Look At Point", target.lookAtPoint);
    13.                  if (GUI.changed)
    14.                  {
    15.                         EditorUtility.SetDirty (target);
    16.              }
    17.          }
    18. }
    19.  
    20.  
    ..gives me this error:

    "Type `UnityEngine.Object' does not contain a definition for `lookAtPoint' and no extension method `lookAtPoint' of type `UnityEngine.Object' could be found (are you missing a using directive or an assembly reference?)"
     
  4. mgrenier

    mgrenier

    Joined:
    Apr 26, 2011
    Posts:
    57
    You need to cast the target to the Class your Editing (LookAtPoint).

    (target as LookAtPoint).lookAtPoint = ....

    or

    LookAtPoint mycomponent = (LookAtPoint)target; // and start using mycomponent instead of target
     
  5. orionburcham

    orionburcham

    Joined:
    Jan 31, 2010
    Posts:
    491
    aha! Thank you sirs, thank you, thankyou