Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Steve Edwards · Jan 30, 2011 at 09:07 AM · editorinspector

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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).

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Steve Edwards · Jan 30, 2011 at 05:04 PM 0
Share

Thanks, that fixed it. Co$$anonymous$$g from C++ I'm still finding my way with some of these class protocols.

avatar image
2

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Steve Edwards · Jan 30, 2011 at 11:38 AM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Call inspector editor script function from another editor script 2 Answers

Drop down with sizes 1 Answer

No enable/disable check box for a script? 3 Answers

Inspector Popup format to look like tag or layer popup 1 Answer

Is there a way to add another slot for a script in the Inspector? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges