docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Class ObservableAttribute

    Specify that a field or property should be used to generate observations for an Agent. For each field or property that uses ObservableAttribute, a corresponding ISensor will be created during Agent initialization, and this sensor will read the values during training and inference.

    Inheritance
    object
    Attribute
    ObservableAttribute
    Inherited Members
    Attribute.Equals(object)
    Attribute.GetCustomAttribute(Assembly, Type)
    Attribute.GetCustomAttribute(Assembly, Type, bool)
    Attribute.GetCustomAttribute(MemberInfo, Type)
    Attribute.GetCustomAttribute(MemberInfo, Type, bool)
    Attribute.GetCustomAttribute(Module, Type)
    Attribute.GetCustomAttribute(Module, Type, bool)
    Attribute.GetCustomAttribute(ParameterInfo, Type)
    Attribute.GetCustomAttribute(ParameterInfo, Type, bool)
    Attribute.GetCustomAttributes(Assembly)
    Attribute.GetCustomAttributes(Assembly, bool)
    Attribute.GetCustomAttributes(Assembly, Type)
    Attribute.GetCustomAttributes(Assembly, Type, bool)
    Attribute.GetCustomAttributes(MemberInfo)
    Attribute.GetCustomAttributes(MemberInfo, bool)
    Attribute.GetCustomAttributes(MemberInfo, Type)
    Attribute.GetCustomAttributes(MemberInfo, Type, bool)
    Attribute.GetCustomAttributes(Module)
    Attribute.GetCustomAttributes(Module, bool)
    Attribute.GetCustomAttributes(Module, Type)
    Attribute.GetCustomAttributes(Module, Type, bool)
    Attribute.GetCustomAttributes(ParameterInfo)
    Attribute.GetCustomAttributes(ParameterInfo, bool)
    Attribute.GetCustomAttributes(ParameterInfo, Type)
    Attribute.GetCustomAttributes(ParameterInfo, Type, bool)
    Attribute.GetHashCode()
    Attribute.IsDefaultAttribute()
    Attribute.IsDefined(Assembly, Type)
    Attribute.IsDefined(Assembly, Type, bool)
    Attribute.IsDefined(MemberInfo, Type)
    Attribute.IsDefined(MemberInfo, Type, bool)
    Attribute.IsDefined(Module, Type)
    Attribute.IsDefined(Module, Type, bool)
    Attribute.IsDefined(ParameterInfo, Type)
    Attribute.IsDefined(ParameterInfo, Type, bool)
    Attribute.Match(object)
    Attribute.TypeId
    object.Equals(object, object)
    object.GetType()
    object.MemberwiseClone()
    object.ReferenceEquals(object, object)
    object.ToString()
    Namespace: Unity.MLAgents.Sensors.Reflection
    Assembly: Unity.ML-Agents.dll
    Syntax
    [AttributeUsage(AttributeTargets.Property|AttributeTargets.Field)]
    public class ObservableAttribute : Attribute
    Remarks

    ObservableAttribute is intended to make initial setup of an Agent easier. Because it uses reflection to read the values of fields and properties at runtime, this may be much slower than reading the values directly. If the performance of ObservableAttribute is an issue, you can get the same functionality by overriding CollectObservations(VectorSensor) or creating a custom ISensor implementation to read the values without reflection.

    Note that you do not need to adjust the VectorObservationSize in BrainParameters when adding ObservableAttribute to fields or properties.

    Examples

    This sample class will produce two observations, one for the m_Health field, and one for the HealthPercent property.

    using Unity.MLAgents;
    using Unity.MLAgents.Sensors.Reflection;
    
    public class MyAgent : Agent
    {
        [Observable]
        int m_Health;
    
        [Observable]
        float HealthPercent
        {
            get => return 100.0f * m_Health / float(m_MaxHealth);
        }
    }

    Constructors

    ObservableAttribute(string, int)

    ObservableAttribute constructor.

    Declaration
    public ObservableAttribute(string name = null, int numStackedObservations = 1)
    Parameters
    Type Name Description
    string name

    Optional override for the sensor name. Note that all sensors for an Agent must have a unique name.

    int numStackedObservations

    Number of frames to concatenate observations from.

    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)