- Home /
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.
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.
Answer by PixelMuncher · Aug 10, 2011 at 01:36 PM
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.
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 )
Answer by PixelMuncher · Jan 27, 2012 at 07:45 AM
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.
Answer by Waz · Aug 10, 2011 at 01:24 PM
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.
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612173928im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Unity Editor - Create a custom scene view 0 Answers
Custom Editor window loses values on play. 1 Answer
How do I create custom editor window layout through script ? 0 Answers
Distribute terrain in zones 3 Answers