Search Unity

Error while trying to play an animation (Javascript)

Discussion in 'Scripting' started by ics_de, Dec 6, 2017.

  1. ics_de

    ics_de

    Joined:
    Dec 6, 2017
    Posts:
    27
    I'm trying to play (every random amount of time) an animation with this script:

    Captura de pantalla 2017-12-06 17.06.28.png

    But I always get this error:

    Captura de pantalla 2017-12-06 17.07.10.png

    Note: I've just started with both Unity and Javascript
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    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 six ways from Sunday. It's also dying. 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:
    Code (csharp):
    1. var i=1;
    2. while (i<4) {
    3. 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:
    Code (csharp):
    1. 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:
    Code (csharp):
    1. 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.
     
    ics_de likes this.
  3. ics_de

    ics_de

    Joined:
    Dec 6, 2017
    Posts:
    27
    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]
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    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:

    Code (csharp):
    1. public void CoolPublicMethodThatReturnsNothing()
    2. {
    3.     Debug.Log("So cool!");
    4. }
    Above is a method that can be called from outside of its class (public) and returns nothing.

    Code (csharp):
    1. private string coolPrivateMethodThatReturnsAString()
    2. {
    3.     string myString = "Still cool!";
    4.     return myString;
    5. }
    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.
     
    Last edited: Dec 6, 2017
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    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 Animation class, when you should be using the Animator 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:

    Code (csharp):
    1. void Start() {
    2. StartCoroutine(AnimationLooper() );
    3. }
    4.  
    5. IEnumerator AnimationLooper() {
    6. while (true) {
    7. GetComponent<Animator>().SetTrigger("StartAnim");
    8. yield return new WaitForSeconds(Random.Range(5f, 20f) );
    9. GetComponent<Animator>().SetTrigger("StartAnim");
    10. }
    11. }
     
  6. SteveJ

    SteveJ

    Joined:
    Mar 26, 2010
    Posts:
    3,085
    You spelt "Scripts" wrong. As in your folder name. You should fix that. Because my OCD.
     
  7. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Start can be a Coroutine. Awake can't.

    Then just do
    Code (csharp):
    1.  
    2. while (true)
    3.  
     
  8. ics_de

    ics_de

    Joined:
    Dec 6, 2017
    Posts:
    27
    Thanks, I didn't notice
     
  9. ics_de

    ics_de

    Joined:
    Dec 6, 2017
    Posts:
    27
    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?
     
    Last edited: Dec 7, 2017