0
\$\begingroup\$

engine: unity

coding language: c#

I am trying to control CinemachineFreeLook maxSpeed from another script in the struct AxisStat: public AxisState(float minValue, float maxValue, bool wrap, bool rangeLocked, float maxSpeed, float accelTime, float decelTime, string name, bool invert) It only accepts to type a number, but not a variable.

Whenever I create a public float variable and place it in maxSpeed, it keeps sending the error: error CS0236: A field initializer cannot reference the non-static field, method, or property 'CinemachineFreeLook.rSpeed'

Here is the default unity CinemachineFreeLook script code + the added code which I declare a float variable "rSpeed" at the beginning and added my variable in the AxisStat maxSpeed:

public class CinemachineFreeLook : CinemachineVirtualCameraBase
{
    public float rSpeed = 300f;

    /// <summary>Object for the camera children to look at (the aim target)</summary>
    [Tooltip("Object for the camera children to look at (the aim target).")]
    [NoSaveDuringPlay]
    public Transform m_LookAt = null;

    /// <summary>Object for the camera children wants to move with (the body target)</summary>
    [Tooltip("Object for the camera children wants to move with (the body target).")]
    [NoSaveDuringPlay]
    public Transform m_Follow = null;

    /// <summary>If enabled, this lens setting will apply to all three child rigs, otherwise the child rig lens settings will be used</summary>
    [Tooltip("If enabled, this lens setting will apply to all three child rigs, otherwise the child rig lens settings will be used")]
    [FormerlySerializedAs("m_UseCommonLensSetting")]
    public bool m_CommonLens = true;

    /// <summary>Specifies the lens properties of this Virtual Camera.
    /// This generally mirrors the Unity Camera's lens settings, and will be used to drive
    /// the Unity camera when the vcam is active</summary>
    [FormerlySerializedAs("m_LensAttributes")]
    [Tooltip("Specifies the lens properties of this Virtual Camera.  This generally mirrors the Unity Camera's lens settings, and will be used to drive the Unity camera when the vcam is active")]
    [LensSettingsProperty]
    public LensSettings m_Lens = LensSettings.Default;

    /// <summary> Collection of parameters that influence how this virtual camera transitions from
    /// other virtual cameras </summary>
    public TransitionParams m_Transitions;

    /// <summary>Legacy support</summary>
    [SerializeField] [HideInInspector]
    [FormerlySerializedAs("m_BlendHint")]
    [FormerlySerializedAs("m_PositionBlending")] private BlendHint m_LegacyBlendHint;

    /// <summary>The Vertical axis.  Value is 0..1.  Chooses how to blend the child rigs</summary>
    [Header("Axis Control")]
    [Tooltip("The Vertical axis.  Value is 0..1.  Chooses how to blend the child rigs")]
    [AxisStateProperty]
    public AxisState m_YAxis = new AxisState(0, 1, false, true, 2f, 0.2f, 0.1f, "Mouse Y", false);

    /// <summary>Controls how automatic recentering of the Y axis is accomplished</summary>
    [Tooltip("Controls how automatic recentering of the Y axis is accomplished")]
    public AxisState.Recentering m_YAxisRecentering = new AxisState.Recentering(false, 1, 2);

    /// <summary>The Horizontal axis.  Value is -180...180.  This is passed on to the rigs' OrbitalTransposer component</summary>
    [Tooltip("The Horizontal axis.  Value is -180...180.  This is passed on to the rigs' OrbitalTransposer component")]
    [AxisStateProperty]
    public AxisState m_XAxis = new AxisState(-180, 180, true, false, rSpeed, 0.1f, 0.1f, "Mouse X", true);
\$\endgroup\$
0
\$\begingroup\$

The compiler error message is telling you exactly what you need to know:

A field initializer cannot reference the non-static field, method, or property

This line is a field initializer:

 public AxisState m_XAxis = new AxisState(-180, 180, true, false, rSpeed, 0.1f, 0.1f, "Mouse X", true);

Here, you're assigning a value to a field, initializing it, during initialization and construction of the component. Because the component isn't fully-constructed yet, you're not yet allowed to reference any members that belong to this instance. So that means trying to use rspeed in this expression is off-limits.

So, you can apply these values in a callback method, after the object is fully initialized:

// Declare, without initializing (or initialize with a constant).
public AxisState m_XAxis;

 // Initialize the variable once the object is fully ready.
void OnValidate() {

    m_XAxis = new AxisState(-180, 180, true, false, rSpeed, 0.1f, 0.1f, "Mouse X", true);
}

OnValidate will run any time you change the value of rSpeed in the Inspector, automatically updating the axes to use its latest value. That value will then be serialized with the component for use in standalone builds.

It is not called in a standalone build, so if you want this to update reactively as the value changes at runtime, you'll need to put this code in Update or invoke a setter/refresh method when you change its value via script.

Moral of the story:

  • Read your compiler error messages carefully to be sure you understand what they're telling you.

  • Search for existing Q&A about your error to find explanations when in doubt.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.