Add Components at runtime

The asset wasn’t really designed with AddComponent in mind. Adding a SpringComponent in the Inspector, setting it up and optionally referencing it from some other script is always the ideal recommended use case.

But if, for some reason, you want or need to add a SpringComponent at runtime this is how you do it:

  1. Add a SpringComponent in runtime

  2. Setup the SpringComponent

  3. Initialize the SpringComponent

  4. Use it normally

Here 2 examples:

colorSpring = gameObject.AddComponent<ColorSpringComponent>();
colorSpring.SetAutoUpdateToTrue(isRenderer: false, uiImage.gameObject);
colorSpring.Initialize();
colorSpring.SetCurrentValue(Color.white);
colorSpring.SetTarget(Color.yellow);

And:

transformSpringComponent = uiImage.gameObject.AddComponent<TransformSpringComponent>();
transformSpringComponent.useTransformAsTarget = true;
transformSpringComponent.followerTransform = uiImage.transform;
transformSpringComponent.targetTransform = uiImageTarget.transform;
transformSpringComponent.SpringScaleEnabled = false;
transformSpringComponent.Initialize();

You can find the full example from above in:

Assets\AllIn1SpringsToolkit\Demo\Scripts\ExampleAddComponent.cs

Last updated