x


Setting variables in public class to values from editor script

So I created my first Editor script. The script opens up a window with an object field and allows me to make several changes to the object through toggle switches and EnumPopups. I click a button and it applies all the attributes I set to the object I placed in the object field. It works like a charm, however the editor script performs some calculations based on the attributes I set that I would like to have access to in the script that is applied to the object.

Essentially, what I would like to be able to do is have another class that has variables to hold the values that my Editor script calculated so that the script that is applied to the object can have access to these values without having to do the calculations itself.

For example, my editor script will have something like this:

//some calculation
private float CalcSomething()
{
    someCalculation = //perform some calculation
    return someCalculation;
}
//this function gets called when I press the button to set the objects attributes
private void SetObjProperties()
{
     ObjProperties.mCalc = CalcSomething();
}

Then in my ObjProperties.cs

public class ObjProperties
{
     //container for calculation from the editor script
     public float mCalc;
}

Then some other MonoBehaviour script that is actually applied to the object can have access to this value by saying something like:

float someVal = ObjProperties.mCalc;

Problem is, this doesn't work. mCalc always has it's default value of 0.0f.

What am I doing wrong?

Any help is greatly appreciated. This is driving me crazy.

more ▼

asked Aug 10 '11 at 05:19 AM

PixelMuncher gravatar image

PixelMuncher
29 3 3 5

You say always - have you stepped through the code to see whether mCalc ever has a value other than 0.0f? Also, remember that in your pseudocode above objProperties is a reference type and not a value type, so if you're passing it around and not copying it, you probably want to start making a copy in SetObjProperties(). Naturally if you zero mCalc somewhere after you've returned it then it's going to change.

Aug 10 '11 at 07:12 AM Bovine
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

After lots of poking around I finally found what I needed here: The skinny on ScriptableObjects. This method lets me store all my precomputed data in an asset file that I can access through other scripts, which is exactly what I want.

more ▼

answered Aug 10 '11 at 01:36 PM

PixelMuncher gravatar image

PixelMuncher
29 3 3 5

The link you provided seems to be dead now. If you ever do return and find this comment, perhaps you could share the gist of that link's info. +1 for finding the answer though (and to hopefully get your attention, :P )

Jan 27 '12 at 05:53 AM Kilometers
(comments are locked)
10|3000 characters needed characters left

You can't store properties just as class variables (I'm assuming you mistranscribed a static, since your code wouldn't even compile otherwise) and expect them to persist. They'll be trashed if that class is reloaded, and obviously will be lost on restart.

Simplest would be to store the calculated value in the actual scene object that ultimately needs it, not try to poke it elsewhere.

more ▼

answered Aug 10 '11 at 01:24 PM

Waz gravatar image

Waz
6.4k 22 33 70

Yeah, my pseudocode was a bit rushed. ^_^

I found what I needed and posted it as an answer. I tested it and it works. Thanks for your time though.

Aug 10 '11 at 01:39 PM PixelMuncher
(comments are locked)
10|3000 characters needed characters left

Certainly Kilometers. First you will need to create a class that extends ScriptableObject (make sure you have using UnityEngine at the top) for your stored variables. It should look something like this:

public class StoredData : ScriptableObject
{
    public float myStoredFloat;
}

Then, in your editor script have some method you call such as this one.

private void StoreData()
{
    //create an instance of your StoredData class
    StoredData storedData = (StoredData)ScriptableObject.CreateInstance("StoredData");
    //create an asset file at given path
    AssetDatabase.CreateAsset(storedData, "Assets/storeddata.asset");
    //this basically tells Unity that our scriptable object variable has changed and needs to be saved
    EditorUtility.SetDirty(storedData);
    //finally assign value to stored variable
    storedData.myStoredFloat = 1f;
}

What this will do is create an asset file that you can reference through other scripts and thus have access to your stored values. So somewhere in your MonoBehaviour have:

public StoredData storedData;

and simply drag the storeddata.asset file into the object field in the inspector.

That pretty much sums it up. If you have any further questions on how this works just let me know.

more ▼

answered Jan 27 '12 at 07:45 AM

PixelMuncher gravatar image

PixelMuncher
29 3 3 5

(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:

x4142
x347
x168
x58
x46

asked: Aug 10 '11 at 05:19 AM

Seen: 1823 times

Last Updated: Jan 27 '12 at 07:48 AM