How to control animation with UI slider @ Unity5.1 ?

Although I’m not programmer, I want to make the demo that controls animation with UI slider, like below.

I tried the script in the above, but it didn’t work. The following is Error message.
If anyone know how to solve, please tell me.

version : Unity 5.1.1 p

— Error 1
UnityException: You are not allowed to call this function when declaring a variable.
Move it to the line after without a variable declaration.

If you are using C# don’t use this function in the constructor or field initializers, Instead move initialization to the Awake or Start function.

UnityEngine.GameObject…ctor (System.String name) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineGameObjectBindings.gen.cs:414)
ScrollbarAction…ctor () (at Assets/ScrollbarAction.js:5)

— Error 2
Internal_CreateGameObject can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

— Error 3
ArgumentException: Internal_CreateGameObject can only be called from the main thread.
Constructors and field initializers will be executed from the loading thread when loading a scene.
Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.

UnityEngine.GameObject…ctor (System.String name) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/UnityEngineGameObjectBindings.gen.cs:414)
ScrollbarAction…ctor () (at Assets/ScrollbarAction.js:5)

from those errors I think you’re trying to use JS code in a C# script…

Thank you for your reply, but I use a Java script…

If you would post your script it would be easier to help. :slight_smile:

Hi traderain, Thank you for your comment :slight_smile:
Then I post the code.

#pragma strict

import UnityEngine.UI;
var obj = GameObject("");
var animName = obj.GetComponent.<Animation>();
         var whatAnimation = "";
        
var animWrapMode = WrapMode.PingPong;
var SliderPos : float;
var animationSlider = GetComponent(UnityEngine.UI.Slider);
function Start () {
    
     animationSlider.direction = Slider.Direction.LeftToRight;
     animationSlider.minValue = 0;
     animationSlider.maxValue = whatAnimation.Length;
    
     animName[whatAnimation].enabled = true;
     animName[whatAnimation].weight = 1;
     animName[whatAnimation].wrapMode = animWrapMode;
        
     }
    
function Update() {
     animName[whatAnimation].normalizedTime = SliderPos;
     }
function moveSlider(newPos : float) {
     SliderPos = newPos;
}