x


Get variables from custom editor to script

How Do i get the variables from the customEditor to my script.

// The Editor Class:
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Weapon))]
public class MyWindow : Editor {
    string myString = "Hello World";
    bool groupEnabled;
    bool myBool = true;
    float myFloat = 1.23f;




    public override void OnInspectorGUI()
    {
        GUILayout.Label ("Base Settings", EditorStyles.boldLabel);
        myString = EditorGUILayout.TextField ("Text Field", myString);

        groupEnabled = EditorGUILayout.BeginToggleGroup ("Optional Settings", groupEnabled);
        myBool = EditorGUILayout.Toggle ("Toggle", myBool);
        myFloat = EditorGUILayout.Slider ("Slider", myFloat, -3, 3);
        EditorGUILayout.EndToggleGroup ();

    }

// The Class the using the editor. This script says that the variables dosent exist

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Weapon : MonoBehaviour {

    void Start()
    {
        Debug.Log(myString );
    }
 }
more ▼

asked May 07 '11 at 09:19 AM

Victor 1 gravatar image

Victor 1
87 13 13 20

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

The problem is you are changing seetings on your editor itself rather than the object you want to edit. You need to move all the properties over to Weapon and make them public. Then use the target property of the editor to get at the instance of the Weapon you are editing.

public class Weapon : MonoBehaviour {
    public string myString = "Hello World";
    public bool groupEnabled;
    public bool myBool = true;
    public float myFloat = 1.23f;

    void Start()
    {
        Debug.Log(myString );
    }
 }

[CustomEditor(typeof(Weapon))]
public class MyWindow : Editor {
    public override void OnInspectorGUI()
    {
        Weapon weapon = (Weapon)target;

        GUILayout.Label ("Base Settings", EditorStyles.boldLabel);
        weapon.myString = EditorGUILayout.TextField ("Text Field", weapon.myString);

        weapon.groupEnabled = EditorGUILayout.BeginToggleGroup ("Optional Settings", weapon.groupEnabled);
        weapon.myBool = EditorGUILayout.Toggle ("Toggle", weapon.myBool);
        weapon.myFloat = EditorGUILayout.Slider ("Slider", weapon.myFloat, -3, 3);
        EditorGUILayout.EndToggleGroup ();

    }
}
more ▼

answered May 07 '11 at 06:29 PM

loramaru gravatar image

loramaru
389 1 6

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1673
x343
x199

asked: May 07 '11 at 09:19 AM

Seen: 1522 times

Last Updated: May 31 '11 at 05:51 AM