0
\$\begingroup\$

In the top of the script:

public AnimationCurve curve;

Then the object in the editor have AnimationCurve but without any curve yet:

Animationcurve

I want to set one of the curves via the script in the Start.

For example to select this one the most one in the right:

AnimationCurve

\$\endgroup\$
1

1 Answer 1

0
\$\begingroup\$

In top of script:

public AnimationCurve curve;

In Start:

public void Start()
    {
      if(curve == null)
        {
          curve = new AnimationCurve(new Keyframe(0, 0), new Keyframe(1, 1));
        }
        curve.preWrapMode = WrapMode.Clamp;
        curve.postWrapMode = WrapMode.Clamp;
    }
\$\endgroup\$
1
  • \$\begingroup\$ You might want to first check whether the curve is null / empty before replacing it. One of the advantages of a public variable in a MonoBehaviour is that it allows a developer to customize the initial values inside the Unity editor's inspector, if they want custom behaviour on a particular instance. So you can preserve that capability by not stomping the curve if someone's already provided a custom one. \$\endgroup\$
    – DMGregory
    Commented Nov 19, 2018 at 3:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .