- Home /
Can't access Inspector variable in C# Editor script?
Hi,
I'm trying to create an Editor script that uses a reference to a Material in the Inspector.
In the following code, the variable 'myMat' appears in the Inspector, but I get an error "An object reference is required to access non-static member `TestScript.myMat'" at the end where I am trying to access the variable. If I make myMat static, I no longer get the error, but myMat is not shown in the Inspector.
using UnityEngine; using UnityEditor; using System.Collections; [System.Serializable]
public class TestScript : ScriptableObject { public Material myMat;
class OtherClass
{
public OtherClass (Material mat)
{
Material classMat = mat;
}
public MakeStuff(/*Big function that makes stuff with myMat*/);
}
[MenuItem ("GameObject/TestScript")]
static void MenuTestScript()
{
OtherClass newClass = new OtherClass(myMat);
newClass.MakeStuff();
}
}
Is there a way to satisfy both? Thanks.
Answer by Matthew A · Jan 30, 2011 at 03:07 PM
AFAIK, static variables are simply not serializeable. You will need to actually create a TestScript object instance in order to get hold of that value.
e.g.
static void MenuTestScript()
{
TestScript test = new TestScript();
OtherClass newClass = new OtherClass(test.myMat)
}
(Of course you may need to refactor your classes if you already use a TestScript instance for other purposes).
Thanks, that fixed it. Co$$anonymous$$g from C++ I'm still finding my way with some of these class protocols.
Answer by Tetrad · Jan 30, 2011 at 10:33 AM
A little more background on what you're specifically trying to do would be helpful.
Are scriptable wizards out of the question for your needs? http://unity3d.com/support/documentation/ScriptReference/ScriptableWizard.html If you want an editor script to "do something" with a material, you can do that pretty easily.
O$$anonymous$$, I stripped it down because it's a big class that makes mesh objects and applies the Inspector-provided $$anonymous$$aterials. I've edited my post to show that a function called $$anonymous$$akeStuff() is called that needs access to the materials. I have done this by creating some class variables and assigning them in the constructor, but I can use any other method if there's a workaround so $$anonymous$$akeStuff() knows about them. Thanks for the link, though I don't think it can help in this case.