Search Unity

Custom Inspector modify reference object's value

Discussion in 'Scripting' started by TheHoboCoder, Oct 20, 2016.

  1. TheHoboCoder

    TheHoboCoder

    Joined:
    Feb 18, 2012
    Posts:
    33
    Hi,

    I think I am missing something here. I have got following script with custom inspector:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class Example : MonoBehaviour {
    6.     public Text text1;
    7. }
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEditor;
    4.  
    5. [CustomEditor(typeof(Example))]
    6. public class ExampleInspector : Editor {
    7.     SerializedProperty text1;
    8.  
    9.     void OnEnable () {
    10.         text1 = serializedObject.FindProperty ("text1");
    11.     }
    12.  
    13.     public override void OnInspectorGUI () {
    14.         serializedObject.Update ();
    15.  
    16.         EditorGUILayout.PropertyField (text1);
    17.         if (text1.objectReferenceValue != null) {
    18.             SerializedObject _text1 = new SerializedObject (text1.objectReferenceValue);
    19.             SerializedProperty t1 = _text1.FindProperty ("text");
    20.             EditorGUILayout.PropertyField (t1);
    21.         }
    22.  
    23.         serializedObject.ApplyModifiedProperties ();
    24.     }
    25. }
    What I want to achieve, is that I will have textfield in Example's inspector and I can modify Unity's Text component "text" value from that inspector. My current inspector script gives me following error when I select Text1:

    So, can anyone point out what I am missing here / doing wrong?
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,698
    Are you sure that your Text class has a property exactly named "text"? It's always a good idea to check for null whenever using a function that can return null. For example:

    Code (csharp):
    1. if (text1.objectReferenceValue != null) {
    2.     SerializedObject _text1 = new SerializedObject (text1.objectReferenceValue);
    3.     _text1.Update();
    4.     SerializedProperty t1 = _text1.FindProperty ("text");
    5.     if (t1 == null) {
    6.         EditorGUILayout.LabelField("Can't find field 'text' in assigned Text object.");
    7.     } else {
    8.         EditorGUILayout.PropertyField (t1);
    9.     }
    10.     _text1.ApplyModifiedProperties();
    11. }
    Also, I added _text1.Update() and ApplyModifiedProperties(). Otherwise your changes won't be reflected in the Text object.
     
  3. TheHoboCoder

    TheHoboCoder

    Joined:
    Feb 18, 2012
    Posts:
    33
    I am trying to access text value of UnityEngine.UI.Text without any success.
     
  4. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
    You need to reference that namespace. Add:
    using UnityEngine.UI;
     
  5. TheHoboCoder

    TheHoboCoder

    Joined:
    Feb 18, 2012
    Posts:
    33
    UnityEngine.UI is already referenced. Problem is that following line returns null:
    Code (CSharp):
    1. SerializedProperty t1 = _text1.FindProperty ("text");
     
  6. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
    It's text a probably referenced (get/setter) or set up differently. Regardless, this will work:
    Code (CSharp):
    1.     public override void OnInspectorGUI()
    2.         {
    3.             m_Obj.Update();
    4.                if(script.text_field != null)
    5.                 {
    6.                     Text textRef = m_Obj.FindProperty("text_field").objectReferenceValue as Text;
    7.                     textRef.text = EditorGUILayout.TextField("LABEL",textRef.text);              
    8.                 }      
    9.             m_Obj.ApplyModifiedProperties();
    10.         }
    There is one minor hitch, It works perfectly and does exactly as expected, but it won't repaint the scene, The values on the component are updated instantly, but just not displayed on the screen. Clicking on the component will update it. I'm sure there is a way to force a refresh, It's just late.
     
    arithmechick102 likes this.
  7. arithmechick102

    arithmechick102

    Joined:
    Aug 22, 2018
    Posts:
    2
    For anyone who comes across this post in the future, Text.m_text is the serialized field that needs to be referenced.

    Text.text is a getter/setter as zombie predicted.

    Code (CSharp):
    1. SerializedProperty t1 = _text1.FindProperty ("m_text");
     
    o0_Chromium_0o likes this.
  8. o0_Chromium_0o

    o0_Chromium_0o

    Joined:
    Sep 13, 2021
    Posts:
    5
    Does text UI update in the scene after editing?