Search Unity

Resolved Is there any way to reference a value of another script in the inspector

Discussion in 'Editor & General Support' started by BrownBrew, Mar 21, 2023.

  1. BrownBrew

    BrownBrew

    Joined:
    Jan 6, 2023
    Posts:
    13
    Hi!
    Basically I'm trying to put everything in one place. For example, I have an enemy
    1_1.jpg
    Each of the damageboxes has it's own script
    1_2.jpg
    Instead of clicking every damagebox and setting up the damage, I'd like the Enemy script to have all the values as reference in the inspector, something like that:
    1_3.jpg

    Is there any way to achieve that? Thank you in advance!
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    7,935
    Definitely possible to achieve by coding your own custom inspectors. There's a lot of resources out there on how to do that.

    Alternatively, you could just use a collection of a plain C# class, which has a serialised field for a game object (or any other relevant component) and this damage value.
     
    BrownBrew likes this.
  3. BrownBrew

    BrownBrew

    Joined:
    Jan 6, 2023
    Posts:
    13
    I found an example to achieve this and it works almost perfectly, but...the problem now is that for some reason it doesn't update the changes unless I Open the prefab I'm trying to change. Once it's open, it works as intended - changing the value in the parent object does so in the child and vice versa. But when I go back to the scene and select the prefab in the assets folder and try to change the damage values - it does not update it. How can I fix this?
    Here's the code I used for the custom editor:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEditor;
    3.  
    4. [CustomEditor(typeof(EnemyProjectile), true)]
    5. public class CIEnemyProjectileDamage : Editor
    6. {
    7.     public override void OnInspectorGUI()
    8.     {
    9.         base.OnInspectorGUI();
    10.  
    11.         EnemyProjectile enemyProjectile = target as EnemyProjectile;
    12.         EnemyDamageBox damageBox = enemyProjectile.GetComponentInChildren<EnemyDamageBox>();
    13.  
    14.         EditorGUILayout.Separator();
    15.         GUILayout.Label("[DamageBox]");
    16.         if (damageBox != null)
    17.         {
    18.             SerializedObject damageBoxSO = new SerializedObject(damageBox);
    19.             if (damageBoxSO != null)
    20.             {
    21.                 SerializedProperty prop = damageBoxSO.GetIterator();
    22.                 while (prop.NextVisible(true))
    23.                 {
    24.                     if (prop.name != "m_Script") EditorGUILayout.PropertyField(prop);
    25.                 }
    26.                 prop.Reset();
    27.                 damageBoxSO.ApplyModifiedProperties();
    28.             }
    29.         }
    30.     }
    31. }
     
  4. BrownBrew

    BrownBrew

    Joined:
    Jan 6, 2023
    Posts:
    13
    I managed to find a solution that seems to work, although I don't really understand what it does in this particular case
    Basically, before I call this:
    Code (CSharp):
    1. damageBoxSO.ApplyModifiedProperties();
    I added this line:
    Code (CSharp):
    1. if (damageBoxSO.hasModifiedProperties) EditorUtility.SetDirty(enemyProjectile);
    (if you don't check for hasModifiedProperties the inspector will stuck in infinite loop of what it looks like updating the prefabs values)
    Now editing damage values are saved even without opening the prefab. Not sure if it's the right way to do that, but it works. The only problem here is that you can't use Undo to revert changes you made because they are not recorded, but I don't really mind that.