# Error while trying to play an animation (Javascript)

**URL:** https://discussions.unity.com/t/error-while-trying-to-play-an-animation-javascript/684897
**Category:** C# & .NET
**Tags:** Scripting, Mono
**Created:** [December 6, 2017, 4:19pm UTC](https://discussions.unity.com/t/error-while-trying-to-play-an-animation-javascript/684897 "2017-12-06T16:19:42Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![ics\_de](https://avatars.discourse-cdn.com/v4/letter/i/6de8d8/32.png) [@ics\_de](https://discussions.unity.com/u/ics_de)
#### Post date: [December 6, 2017, 4:19pm UTC](https://discussions.unity.com/t/error-while-trying-to-play-an-animation-javascript/684897/1 "2017-12-06T16:19:42Z")

</div>

I’m trying to play (every random amount of time) an animation with this script:

![3313265--257512--Captura de pantalla 2017-12-06 17.06.28.png](https://europe1.discourse-cdn.com/unity/original/4X/0/4/0/040972d30216ef1b288ed6c851e8e294f36b47d7.png)

But I always get this error:

![3313265--257513--Captura de pantalla 2017-12-06 17.07.10.png](https://europe1.discourse-cdn.com/unity/original/4X/5/1/6/51697d0d055c056a4af149d7a55f716cb1757201.png)

Note: I’ve just started with both Unity and Javascript

---

<div class="post-metadata">

### Author: ![StarManta](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/starmanta/32/280327_2.png) [@StarManta](https://discussions.unity.com/u/StarManta)
#### Post date: [December 6, 2017, 5:33pm UTC](https://discussions.unity.com/t/error-while-trying-to-play-an-animation-javascript/684897/2 "2017-12-06T17:33:06Z")

</div>

There are a number of issues with this script. I’ll deal with the actual error message last.

First thing: **don’t use JavaScript**. [It’s awful](https://starmanta.gitbooks.io/unitytipsredux/content/c-vs-javascript.html) six ways from Sunday. It’s also [dying](https://blogs.unity3d.com/2017/08/11/unityscripts-long-ride-off-into-the-sunset/). Since you’re just learning, abandon whatever old tutorial you’re following that’s having you use Javascript, and find one that uses C# instead (like any of the official tutorials, for example)

Beyond that, there are two issues with the script itself. Your “while” loop is just going to loop forever, because “1\<4” will always be true. I’m guessing you meant to do something like:

```auto
var i=1;
while (i<4) {
i++;

```

Which will run the code 4 times. Well… actually 3 times, because you started at 1. 99.9% of the time in coding, you want to start counting at 0.

Now, the error message itself is because you’re using the wrong function of Random. What you’re trying to use is:

```auto
Random.Range(5,20);

```

Random.value is just used on its own, and it’s just a random number between 0 and 1. So when you have Random.value(5,20), what the compiler sees is something like:

```auto
0.5f(5,20)

```

which is nonsense.

I mostly help with these two issues because these will also be issues when you switch to C#, which you 1000% need to do. Using JS has never been the besst choice, but using it now is just begging for all kinds of heartache.

---

<div class="post-metadata">

### Author: ![ics\_de](https://avatars.discourse-cdn.com/v4/letter/i/6de8d8/32.png) [@ics\_de](https://discussions.unity.com/u/ics_de)
#### Post date: [December 6, 2017, 8:19pm UTC](https://discussions.unity.com/t/error-while-trying-to-play-an-animation-javascript/684897/3 "2017-12-06T20:19:38Z")

</div>

Thank you! I will follow your advice and I will start learning C#.

1. I really wanted it to loop forever, so I won’t change the loop part

2. How should I start translating the C# script? I really don’t understand the difference between “void Start” and “void Update”[/QUOTE]

---

<div class="post-metadata">

### Author: ![Joe-Censored](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/joe-censored/32/419943_2.png) [@Joe-Censored](https://discussions.unity.com/u/Joe-Censored)
#### Post date: [December 6, 2017, 8:36pm UTC](https://discussions.unity.com/t/error-while-trying-to-play-an-animation-javascript/684897/4 "2017-12-06T20:36:38Z")

</div>

> [@ics\_de](#):
>
> Thank you! I will follow your advice and I will start learning C#.
> 
> 1. I really wanted it to loop forever, so I won’t change the loop part
> 
> 2. How should I start translating the C# script? I really don’t understand the difference between “void Start” and “void Update”

1. Preventing your Start method from ever completing is probably going to cause you problems.

2. Start and Update are used for the same purpose in Unityscript (js) as in C#. Start is for some initial setup of your script, such as setting some initial values or establishing some references to other scripts or gameobjects, and Update is called on every frame afterward and is used for your in game logic you need executed on each frame.

In C# instead of declaring a method with “function”, you declare it with the return type, and optionally the scope. The return type of “void” means the method returns nothing. Not specifying the scope is the same as writing “private”.

For example:

```auto
public void CoolPublicMethodThatReturnsNothing()
{
    Debug.Log("So cool!");
}

```

Above is a method that can be called from outside of its class (public) and returns nothing.

```auto
private string coolPrivateMethodThatReturnsAString()
{
    string myString = "Still cool!";
    return myString;
}

```

Above is a method that can only be called within its class (private) and returns a string to the method that calls it.

For more just start with some basic C# tutorials and you should get up to speed with the basics pretty quickly.

---

<div class="post-metadata">

### Author: ![StarManta](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/starmanta/32/280327_2.png) [@StarManta](https://discussions.unity.com/u/StarManta)
#### Post date: [December 6, 2017, 8:48pm UTC](https://discussions.unity.com/t/error-while-trying-to-play-an-animation-javascript/684897/5 "2017-12-06T20:48:22Z")

</div>

One difference between the languages is that for a function to be a coroutine in C# (that is, a function that can “yield”), it must be declared as **IEnumerator SomeFunctionName() {** (instead of using void). In JS it assumes anything with a yield is an IEnumerator, in C# you have to declare it explicitly. I _think_ C# also doesn’t allow Start to be a coroutine, unlike JS, **but** you can call a coroutine from Start no problem.

Another thing I noticed: You’re using the Animat **ion** class, when you should be using the Animat **or** class. Animation is the old way, and I assume you got it from the same old, old tutorial that thought that JS was a good idea 😉 But Animator - part of the “MecAnim” system of animation - is how all animations are imported since Unity 3.x or something. I recommend you check out a tutorial on setting up that component.

Ultimately, your script will end up looking more like this:

```auto
void Start() {
StartCoroutine(AnimationLooper() );
}

IEnumerator AnimationLooper() {
while (true) {
GetComponent<Animator>().SetTrigger("StartAnim");
yield return new WaitForSeconds(Random.Range(5f, 20f) );
GetComponent<Animator>().SetTrigger("StartAnim");
}
}

```

---

<div class="post-metadata">

### Author: ![SteveJ](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/stevej/32/557576_2.png) [@SteveJ](https://discussions.unity.com/u/SteveJ)
#### Post date: [December 6, 2017, 9:11pm UTC](https://discussions.unity.com/t/error-while-trying-to-play-an-animation-javascript/684897/6 "2017-12-06T21:11:11Z")

</div>

> [@ics\_de](#):
>
> I’m trying to play (every random amount of time) an animation with this script:
> 
> But I always get this error:
> 
> Note: I’ve just started with both Unity and Javascript

You spelt “Scripts” wrong. As in your folder name. You should fix that. Because my OCD.

---

<div class="post-metadata">

### Author: ![KelsoMRK](https://dub1.discourse-cdn.com/unity/user_avatar/discussions.unity.com/kelsomrk/32/205687_2.png) [@KelsoMRK](https://discussions.unity.com/u/KelsoMRK)
#### Post date: [December 6, 2017, 9:13pm UTC](https://discussions.unity.com/t/error-while-trying-to-play-an-animation-javascript/684897/7 "2017-12-06T21:13:22Z")

</div>

> [@StarManta](#):
>
> I _think_ C# also doesn’t allow Start to be a coroutine, unlike JS, **but** you can call a coroutine from Start no problem.

Start can be a Coroutine. Awake can’t.

> [@ics\_de](#):
>
> 1. I really wanted it to loop forever, so I won’t change the loop part

Then just do

```auto
while (true)

```

---

<div class="post-metadata">

### Author: ![ics\_de](https://avatars.discourse-cdn.com/v4/letter/i/6de8d8/32.png) [@ics\_de](https://discussions.unity.com/u/ics_de)
#### Post date: [December 7, 2017, 9:50am UTC](https://discussions.unity.com/t/error-while-trying-to-play-an-animation-javascript/684897/8 "2017-12-07T09:50:00Z")

</div>

> [@SteveJ](#):
>
> You spelt “Scripts” wrong. As in your folder name. You should fix that. Because my OCD.

Thanks, I didn’t notice

---

<div class="post-metadata">

### Author: ![ics\_de](https://avatars.discourse-cdn.com/v4/letter/i/6de8d8/32.png) [@ics\_de](https://discussions.unity.com/u/ics_de)
#### Post date: [December 7, 2017, 9:54am UTC](https://discussions.unity.com/t/error-while-trying-to-play-an-animation-javascript/684897/9 "2017-12-07T09:54:31Z")

</div>

> [@StarManta](#):
>
> One difference between the languages is that for a function to be a coroutine in C# (that is, a function that can “yield”), it must be declared as **IEnumerator SomeFunctionName() {** (instead of using void). In JS it assumes anything with a yield is an IEnumerator, in C# you have to declare it explicitly. I _think_ C# also doesn’t allow Start to be a coroutine, unlike JS, **but** you can call a coroutine from Start no problem.
> 
> Another thing I noticed: You’re using the Animat **ion** class, when you should be using the Animat **or** class. Animation is the old way, and I assume you got it from the same old, old tutorial that thought that JS was a good idea 😉 But Animator - part of the “MecAnim” system of animation - is how all animations are imported since Unity 3.x or something. I recommend you check out a tutorial on setting up that component.
> 
> Ultimately, your script will end up looking more like this:
> 
> ```auto
> void Start() {
> StartCoroutine(AnimationLooper() );
> }
> 
> IEnumerator AnimationLooper() {
> while (true) {
> GetComponent<Animator>().SetTrigger("StartAnim");
> yield return new WaitForSeconds(Random.Range(5f, 20f) );
> GetComponent<Animator>().SetTrigger("StartAnim");
> }
> }
> 
> ```

I’ve been thinking about this and I think it would be better if I make a “Car spawner” that generates different types of cars ( and just like in the animation method) in a random amount of time (And of course when the car reaches the end of the road disappears). Is it hard to do?
