Accessing an arbitary float defined via the inspector

Yes, this I am asking the ‘How do I access a variable on another script’ question. :slight_smile:

I’m building a gauge type indicator. Think pressure gauge or temperature indicator. I want to have a script that can be assigned a float variable on another monobehaviour via the inspector. The gauge will then update according to the float.

I can make the gauge update fine. But I’m having trouble figuring out how to assign the variable to ‘watch’ in the inspector.

I’d originally looked at going down the interface path. But this doesn’t allow the flexibility I’m after. It also requires a significant amount of boilerplate code. Using an event/subscription system from the float properties also suffers from similar drawbacks.

I’m thinking it can be done with reflection. Perhaps ending up with something that looks like the 4.6 event trigger box in the inspector, but picking a float instead of a method. Or using the get method.

Either way I have no idea where to start, or even the correct terms to Google. Any help would be much appreciated.

Since variables can’t be directly referenced and you don’t want to use an interface, the only solution left over is using reflection. You could use delegates and use something like this to serialize them in the editor. However, the delegates also have to be created in some way and i’m not sure if lambda expressions and closures will work.

So the easiest and straight forward solution is to use a variables which holds a MonoBehaviour reference and a string variable to define which field you want to access. This functionality can be wrapped in a custom class and implement a property drawer to show a drop-down list of available fields to select from if you like.

The important thing for anything “editable in the inspector” is, that the data in question can be serialized by Unity. Here’s an example helper class:

[System.Serializable]
public class RemoteField<T>
{
    public MonoBehaviour targetScript;
    public string fieldName;
    private System.Reflection.FieldInfo GetFieldInfo()
    {
        if (targetScript == null)
            return null;
        var type = targetScript.GetType();
        return type.GetField(fieldName, System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
    }
    public T Value
    {
        get
        {
            var field = GetFieldInfo();
            if (field != null)
            {
                var val = field.GetValue(targetScript);
                return (T)val;
            }
            return default(T);
        }
        set
        {
            var field = GetFieldInfo();
            if (field != null)
            {
                field.SetValue(targetScript, value);
            }
        }
    }
}

[System.Serializable]
public class RemoteFloatField : RemoteField<float> { };

Keep in mind that Unity can’t serialitze generic types but a type derived from a generic type. So the RemoteFloatField class should be serialized by Unity. It contains a reference to a script and a string containing the field in question. As said above you could write a property drawer so you don’t let the user enter the field name manually (which is error prone due to misspelling) but instead create a dropdown field with the available fields.

So in a class you could declare a field like this:

public class SomeMonoBehaviour : MonoBehaviour
{
    public RemoteFloatField removeField;
    void Update()
    {
        float v = removeField.Value;
        // ...
    }
}

I might have missed the subtlety in your problem here, but can’t you simply have a public variable in your first script of type “otherScript”, and assigning it via the inspector? Then you’d simply be watching otherScript.floatValue?