Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Why doesn't this work?

Discussion in 'Scripting' started by Cooper37, Aug 6, 2014.

  1. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    There's an error in line 5 that doesn't recognise "target".
    eStart.js
    Code (csharp):
    1.  
    2. @CustomEditor(Start)
    3. @CanEditMultipleObjects
    4. class eStart extends EditorWindow{
    5.     private var autoStart : boolean = target.autoStart;
    6.     function OnInspectorGUI(){
    7.         autoStart = EditorGUILayout.Toggle("Auto Start", autoStart);
    8.     }
    9. }
    10.  
    Start.js
    Code (csharp):
    1.  
    2. private var autoStart : boolean = false;
    3. function Start () {
    4.     if(autoStart){
    5.         Debug.Log("Mission sequence has begun...");
    6.     }
    7. }
    8.  
     
    Last edited: Aug 6, 2014
  2. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    You should be more specific as to what your wanting. Are you actually wanting an custom editor window or just a custom editor?

    Also since your using JS(UnityScript) please add in the Script class names because @CustomEditor(Start) looks odd to me since I would never name a class Start, which is a Method in Monobehavior. But your class name easily could be "Start".
     
  3. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    Sorry, should've been more specific. I'm trying to change the inspector with this. The eStart is the class editor script and the Start is just the js.
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Yeah, dunno if 'Start' might cause conflicts, having the class name the same as the method name, eeeh maybe.
    Anyway, its probably the 'target.autoStart' thats giving the error, not that target isn't recognised, but that autoStart is not a property of 'target', target is an Object, not a class reference.
    Cast first, (target as ClassName) then you can access its variables
     
  5. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    The class and method names are fine, it still doesn't work even when I change them.

    I changed it from EditorWindow to Editor. When extends Editor, it doesn't recognize "target", while EditorWindow doesn't recognize autoStart. I had a pragma strict and I took it out of both and kept extends Editor. I also took out the variable in the editor script. While I get no errors this time and the inspector does make a toggle button, it doesn't debug the line in start script:
    eStart.js
    Code (csharp):
    1.  
    2. import UnityEditor;
    3. import UnityEngine;
    4. import System.Collections;
    5. import System.Collections.Generic;
    6. @CustomEditor(Start)
    7. @CanEditMultipleObjects
    8. class eStart extends Editor{
    9.     function OnInspectorGUI(){
    10.         target.autoStart = EditorGUILayout.Toggle("Auto Start", target.autoStart);
    11.     }
    12. }
    13.  
    The start script is the same. Idk why this is so hard seeing how I've done this exact same thing before, I just need to recreate it. Ugh..
     
  6. Ereous

    Ereous

    Joined:
    Aug 29, 2012
    Posts:
    163
    Your variable is private I don't know how its possible for it to set it. I mostly do c# myself. I would probably use a property if I wanted it to be a private variable.

    Code (CSharp):
    1. private bool myVar;
    2.     public bool MyProperty
    3.     {
    4.         get { return myVar; }
    5.         set { myVar = value; }
    6.     }
    And just use the Editor to toggle the property which would set the private variable.
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,398
    Or just make autoStart a public variable. The problem you were having first is because target is not a property of EditorWindow, it's a property of Editor. The problem you were having with #pragma strict is that autoStart is not a property of Object, it's a property of Start, so you should cast it:

    Code (javascript):
    1. (target as Start).autoStart = EditorGUILayout.Toggle("Auto Start", (target as Start).autoStart);
    By the way, almost all of those namespaces you're importing are already imported by default anyway, so you're probably getting warnings. Just remove all the import lines.

    --Eric
     
  8. Cooper37

    Cooper37

    Joined:
    Jul 21, 2012
    Posts:
    383
    Okay, this worked with #pragma strict and the autoStart var public in the Start script. Thanks for the help guys! :)