Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Stratosome · Jul 09, 2018 at 11:38 PM · editoreditor-scriptingnullreferenceexceptiongetcomponenteditorgui

Getting component variables in InitializeOnLoad editor script?

I have a script that I'm trying to run in editor and I have some objects that have a component with data that I want. This is the simple component that is on target objects and I have already set the public string to have a value in the inspector:

 [DisallowMultipleComponent]
 public class HierarchyLabelAssignment : MonoBehaviour {
     public string label;
 }

The issue is in my editor script that has InitializeOnLoad, inside of its constructor, I can manage to retrieve that component just fine, but any value that the variable has is null even if it has a previously set value.

 [InitializeOnLoad]
 public static class HierarchyWindow {
 
     // --- Initialization
     static HierarchyWindow() {
 
         GameObject test = GameObject.Find("Main Camera");
         HierarchyLabelAssignment labelAssignment = test.GetComponent<HierarchyLabelAssignment>();
         if (labelAssignment != null) {
             Debug.Log("TEST: " + labelAssignment.label);
         }
 
     }
 }

Every time I modify the script, the HierarchyWindow "function" is called again obviously. I just can't figure out why the labelAssignment.label is null when it clearly isn't in the inspector.

Any ideas? I don't do a whole lot of editor coding usually.

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

1 Reply

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

Answer by felipin · Jul 09, 2018 at 11:55 PM

Use SerializedObject and SerializedProperty.

 using UnityEditor;
 using UnityEngine;
 
 [InitializeOnLoad]
 public static class HierarchyWindow {
 
    // --- Initialization
    static HierarchyWindow() {
  
       GameObject test = GameObject.Find("Main Camera");
       HierarchyLabelAssignment labelAssignment = test.GetComponent<HierarchyLabelAssignment>();
 
       if (labelAssignment) {
          SerializedObject serializedObject = new SerializedObject(labelAssignment);
          SerializedProperty serializedProperty = serializedObject.FindProperty("label");
 
 
          Debug.Log("TEST: " + serializedProperty.stringValue);
 
       }
    }
  }
 


Comment
Add comment · Show 4 · 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 Stratosome · Jul 10, 2018 at 12:28 AM 0
Share

That code is looking very promising, but serializedProperty.stringValue is still being printed as an empty string... I'll look into it some more.

avatar image Stratosome · Jul 10, 2018 at 12:38 AM 0
Share

Okay, yeah, this way seems to work only if the label variable in the small HierarchyLabelAssignment is initialized inside of the script, so:

 public class HierarchyLabelAssignment : $$anonymous$$onoBehaviour {
      public string label = "my test";
  }

That will work and print out the value with the code you posted. But since that value isn't initialized in the class, it isn't printing out I guess.

avatar image felipin Stratosome · Jul 10, 2018 at 08:09 AM 0
Share

Thats a little bit weird I've tried it and works fine, the printed string was the same value as in inspector. i dont think so but you can try use the method SerializedObject.Update right after instantiated it.

avatar image Stratosome · Jul 10, 2018 at 04:48 AM 0
Share

Alright, after a fair amount of research, I believe I can clarify my issue. Using the method you described in your code, that will retrieve the serialized value of that variable (not sure I'm totally using the word right). The value that is serialized though is whatever the variable is initialized with. This seems to match the behavior I am getting. When I ask for the value of the SerializedProperty, I am getting that initial value. So, my focused question would be: how do I update that serialized value of the variable? I've looked in to SerializedObject.Apply$$anonymous$$odifiedProperties() and SerializedObject.Update() but nothing seems to work. Here is my code at the moment (testing code):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEditor;
 using UnityEngine;
 
 // Placed on "$$anonymous$$ain Camera" for testing
 [ExecuteInEdit$$anonymous$$ode]
 public class SerializationTest : $$anonymous$$onoBehaviour {
     public string newLabel = "Blue";
     public bool changeButton = false; // A "button" for the inspector (just for quick testing)
 
     public void Update() {
         if (changeButton == true) {
             Debug.Log("Changing Label");
             EditorScript.SetSerializedValue(newLabel);
             changeButton = false;
         }
     }
 }
 
 
 [InitializeOnLoad]
 public static class EditorScript {
 
     private static GameObject mainCam;
     private static HierarchyLabelAssignment monoScript;
 
     // --- Initialization
     static EditorScript() {
         mainCam = GameObject.Find("$$anonymous$$ain Camera");
         monoScript = mainCam.GetComponent<HierarchyLabelAssignment>();
 
         PrintSerializedValue();
     }
 
     public static void SetSerializedValue(string newLabel) {
         if (monoScript) {
             monoScript.label = newLabel;
 
             // --- Can't figure out how to save the changed variable!
             SerializedObject serializedObject = new SerializedObject(monoScript);
             SerializedProperty serializedProperty = serializedObject.FindProperty("label");
             serializedObject.Apply$$anonymous$$odifiedProperties();
             EditorUtility.SetDirty(monoScript);
             }
     }
 
     public static void PrintSerializedValue() {
         if (monoScript) {
             SerializedObject serializedObject = new SerializedObject(monoScript);
             SerializedProperty serializedProperty = serializedObject.FindProperty("label");
 
             Debug.Log("Label = " + serializedProperty.stringValue);
         }
     }
 
  }
 

Every time I use the little toggle button in the inspector out of play mode to set the label variable, it seems to set it visually. However, when I go back into the code, make a change, and trigger a recompile, the printed label is still an empty string.

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

128 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

Related Questions

SceneView.onSceneGUIDelegate GUI sorting problem 1 Answer

Custom Editor - Is there any way to detect whether the user is in Prefab editing mode? 1 Answer

Cannot close Preview Scene 0 Answers

GetPropertyHeight infinite recursion on drawer 1 Answer

Can I name a game object's components (in the editor)? 2 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