Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by badatnames16 · Nov 29, 2016 at 09:53 PM · c#custom-inspectorsubclass

Custom inspector, cant edit variables in subclass

So i used a tutorial on how to create a custom inspector because i'm making a farm game and i wanted to make an easier way to create orders for an order board so i have it to where when i press a button on the inspector it adds a Element to a list and under the Elements are multiple variables

 public class OrderAction : MonoBehaviour {
 
     //MyClass myclass;
     materialss material;
 
     public List<materialss> materials = new List<materialss>();
 
     [System.Serializable]
     public class materialss
     {
 
         public string MaterialName = string.Empty;
         public int amountNeeded;
         public Image materialImage;
         public Text amountNeededT;
 
         public void Update()
         {
             Debug.Log("Done");
             materialImage.sprite = Resources.Load(MaterialName) as Sprite;
         }
     }
 }

This is the script for controlling the orders

 [CustomEditor(typeof(OrderAction))]
 public class OrderEditor : Editor
 {
 
     OrderAction t;
     SerializedObject GetTarget;
     SerializedProperty thisList;
 
     void OnEnable()
     {
         t = (OrderAction)target;
         GetTarget = new SerializedObject(t);
         thisList = GetTarget.FindProperty("materials");
     }
 
     public override void OnInspectorGUI()
     {
         OrderAction myOrderAction = (OrderAction)target;
 
         GetTarget.Update();
 
         //myOrderAction.experience = EditorGUILayout.IntField("Experience", myOrderAction.experience);
         GUILayout.Label("List of materials");
         GUILayout.Space(20);
         if (GUILayout.Button("Add Material"))
         {
             t.materials.Add(new OrderAction.materialss());
             Debug.Log("Added");
         }
 
         base.OnInspectorGUI();
     }
 
     public void addMaterial()
     {
         OrderAction myOrderAction = (OrderAction)target;
        
         myOrderAction.materials.Add(new OrderAction.materialss());
         Debug.Log("Added");
     }
 
 }

This is the script that edits the inspector

Now the problem is in the OrderAction script i have a subclass called materialss. When the button is pressed it creates a new element using the variables in the subclass and it creates the new elements fine but i cant seem to be able to edit them. for example I have the Update function and im trying to get the image for the material to change to a sprite according to the name and it doesn't work. i put a Debug.Log in and it doesn't even call that. So how would i be able to edit the variables or can i?

Comment
Add comment · Show 1
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 Bunny83 · Nov 29, 2016 at 10:59 PM 0
Share

Please look up in the userguide how to properly format code here on Unity Answers. I fixed it for you this time.

1 Reply

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

Answer by Bunny83 · Nov 29, 2016 at 10:56 PM

First of all you're mixing to different concepts how custom inspectors can be implemented. You could drop your SerializedObject / SerializedProperty as you don't use it at all.

When using the "target" property you're dealing with your object directly. However that means you have to take care of telling the editor when things have changed so the editor actually uptates the serialized data. This is usually done by using Undo.RecordObject right before you change anything.

So it would look like this:

 [CustomEditor(typeof(OrderAction))]
 public class OrderEditor : Editor
 {
     OrderAction t;
     void OnEnable()
     {
         t = (OrderAction)target;
     }
     public override void OnInspectorGUI()
     {
         if (GUILayout.Button("Add Material"))
         {
             Undo.RecordObject(t, "Add new material");
             t.materials.Add(new OrderAction.materialss());
         }
         base.OnInspectorGUI();
     }
 }


Comment
Add comment · Show 3 · 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 badatnames16 · Nov 29, 2016 at 11:26 PM 0
Share

public class OrderAction : $$anonymous$$onoBehaviour {

  materialss material;
 
  public List<materialss> materials = new List<materialss>();
 
  [System.Serializable]
  public class materialss
  {
 
      public string $$anonymous$$aterialName = string.Empty;
      public int amountNeeded;
      public Image materialImage;
      public Text amountNeededT;
 
      public void Update()
      {
          Debug.Log("working");
      }
  }

}

its still not working. the Debug.Log("working") isnt happening. is there a certain way to perform functions inside a subclass?

avatar image Bunny83 badatnames16 · Nov 29, 2016 at 11:45 PM 0
Share

Uhm, what does your Update method has to do with the custom inspector? It's just a normal method, if you want to call it, call it. Unity won't call it for you as that class is not a $$anonymous$$onoBehaviour.

If you want to invoke Update when the new instance is created you can do:

 var inst = new OrderAction.materialss();
 t.materials.Add(inst);
 inst.Update();

ins$$anonymous$$d. Though if that method is ment only for initializing it should be named "Init()" or something like that.

avatar image badatnames16 Bunny83 · Nov 30, 2016 at 12:21 AM 0
Share

The custom inspector part works fine. But its not letting me change things. Like the custom inspector is to create new items that are needed for the different orders in the game. So it has the name of the item, the icon, and the amount needed. and for example i want to change the text for how much wheat is needed for the order but when i try to nothing happens at all. Or if i'm trying to check to see if my inventory contains the amount needed it just wont do anything. Sorry i'm trying to explain it the best i can.

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

242 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Hide headers and variables in inspector from parent classes 1 Answer

Making a bubble level (not a game but work tool) 1 Answer

[Editor Scripting] Call Function in other GameObjects Editor Script? 0 Answers


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