Search Unity

Problem with animations

Discussion in '2D' started by alfonsojoseguerrero, Jul 19, 2021.

  1. alfonsojoseguerrero

    alfonsojoseguerrero

    Joined:
    Jun 30, 2021
    Posts:
    40
    Hey guys! I am pretty new to C# and am currently making my second game using this language. I am stuck as an error is being shown in the console. The animations doesn´t work.
    I have this warning:

    Parameter 'Istouching' does not exist.
    UnityEngine.Animator:SetBool (string,bool)
    Muelle:Update () (at Assets/Scripts/Muelle.cs:23)

    I explain;
    I have been developing the functionality of a dock in unity, and I have used this script to program it. Everything works correctly, the problem is when configuring the animations.
    What I have done has been to create a variable of type bool called is touching with that it will know when the player touches the dock with his tag.


    upload_2021-7-19_10-28-25.png





    I have also configured the animator so that the animations are displayed at the correct time.
    Dock ---> Dock stretching when istouching = true
    Dock stretching ---> Dock when istouching = false

    I am Spanish, my English is a bit bad. Thanks to anyone who can help ;)

    Muelle = dock . Muelle estirado = dock stretching

    captura-de-pantalla-175.png (204.3 kB)
    captura-de-pantalla-174.png (145.3 kB)
     

    Attached Files:

  2. Epsilon_Delta

    Epsilon_Delta

    Joined:
    Mar 14, 2018
    Posts:
    258
    It would help to see the screenshot of your Animator. The code looks ok, is the name "Istouching" spelled correctly? Keep in mind that it is case-sensitive.
     
  3. alfonsojoseguerrero

    alfonsojoseguerrero

    Joined:
    Jun 30, 2021
    Posts:
    40
    Thanks for answering. Here is the screenshot of the animator.

    upload_2021-7-19_13-24-14.png
     

    Attached Files:

  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    As was said above, it's case-sensitive. The animator has a lower-case "i". You're setting it with an upper case "I".

    "istouching" NOT "Istouching"
     
  5. alfonsojoseguerrero

    alfonsojoseguerrero

    Joined:
    Jun 30, 2021
    Posts:
    40
    Thanks for the reply, I already tried with that but it still doesn't work.

    upload_2021-7-19_15-8-14.png
     

    Attached Files:

  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    Care to elaborate on what this means? You're saying it's still telling you that it cannot find "istouching"?
     
  7. alfonsojoseguerrero

    alfonsojoseguerrero

    Joined:
    Jun 30, 2021
    Posts:
    40
    Exactly this is the error:

    upload_2021-7-19_15-15-46.png
     

    Attached Files:

  8. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    Don't you have that disabled in the image above?
     
  9. alfonsojoseguerrero

    alfonsojoseguerrero

    Joined:
    Jun 30, 2021
    Posts:
    40
    I do not understand what you mean. You speak of the square next to "istouching"?
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    No, not that, that's obviously the bool state. Sorry, on the device I was looking at your image, the GameObject looked disabled. Now I'm on a bigger screen it's clear it's not.

    Only thing I can guess is that you're somehow referring to the wrong animator but that's just a guess.
     
  11. alfonsojoseguerrero

    alfonsojoseguerrero

    Joined:
    Jun 30, 2021
    Posts:
    40
    I don't know what to do, I've been stuck with this for a long time.
     
  12. alfonsojoseguerrero

    alfonsojoseguerrero

    Joined:
    Jun 30, 2021
    Posts:
    40
    What I literally did was create a bouncing material for the spring. Since the bounce it was doing was too powerful, I decided to script inside the dock to limit the height. Also insert the script into the player. Then for the animations create one with the normal spring and another with the stretched spring. I put the conditions with us "istouching" and modify the script.
     
  13. Epsilon_Delta

    Epsilon_Delta

    Joined:
    Mar 14, 2018
    Posts:
    258
    As @MelvMay said, are you referring to the correct animator? Also, check if the animator parameter does not have white space character at the end like this "istouching ".
     
  14. alfonsojoseguerrero

    alfonsojoseguerrero

    Joined:
    Jun 30, 2021
    Posts:
    40
    I don't understand what you mean with "the correct animator".
     
  15. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,487
    Obviously it means you're referring to a specific animator in your code. Does that animator have the animation you're actually showing above? In none of your images do you show this.
     
    alfonsojoseguerrero likes this.
  16. alfonsojoseguerrero

    alfonsojoseguerrero

    Joined:
    Jun 30, 2021
    Posts:
    40
    Obviously
    upload_2021-7-19_16-57-20.png
     

    Attached Files:

  17. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    Have you assigned the transitions to activate depending on the status of the bool? Maybe that warning pops up if they arent assigned yet.
     
  18. alfonsojoseguerrero

    alfonsojoseguerrero

    Joined:
    Jun 30, 2021
    Posts:
    40
    It's all assigned.

    upload_2021-7-19_17-12-30.png



    upload_2021-7-19_17-13-8.png
     

    Attached Files:

  19. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    Okay, I think i know what is going on. You are setting the bool to equal istouching in update on every frame. It is probably resetting the animation trigger each frame so it is never occurring. I would remove the anim.SetBool line in update entirely. Then, in your OnCollisionEnter2D just get rid of the istouching = true/false and replace it with anim.SetBool("istouching", true) and false.

    Additionally, in the else condition you are setting it to false if the tag isnt "Muelle". It could be that when the player is on it that the collider is also touching another gameobject that doesnt have that tag. So...I would remove the else part and then create another function using OnCollisionExit2D to set the anim.SetBool to false.

    Last thing, add in some debug.logs to each if/else that you have to determine if everything/anything is actually running and to see if the bool is true/false when it should be true/false.

    Edit: The reason i believe this is cause you are getting a Yellow warning, not a Red error. The error would make it not work, the warning doesnt mean its null or not compiling, it is kinda just for cleaning up code that is unnecessary.
     
  20. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    Try this:
    Code (CSharp):
    1. public float AlturaMaxima;
    2.  
    3.  
    4.     private Rigidbody2D rb2d;
    5.     private Animator anim;
    6.  
    7.     void Start()
    8.     {
    9.         rb2d = GetComponent<Rigidbody2D>();
    10.         anim = GetComponent<Animator>();
    11.  
    12.  
    13.     }
    14.  
    15.     void OnCollisionEnter2D(Collision2D col)
    16.     {
    17.         if(col.gameObject.tag == "Muelle")
    18.         {
    19.             anim.SetBool("istouching", true);
    20.             Debug.Log("istouching should be true right now");
    21.             rb2d.velocity = new Vector2(rb2d.velocity.x, AlturaMaxima);
    22.         }
    23.     }
    24.  
    25.     void OnCollisionExit2D(Collision2D col)
    26.     {
    27.         if (col.gameObject.tag == "Muelle")
    28.         {
    29.             anim.SetBool("istouching", false);
    30.             Debug.Log("istouching should be FALSE and player is no longer colliding");
    31.         }
    32.     }
     
  21. alfonsojoseguerrero

    alfonsojoseguerrero

    Joined:
    Jun 30, 2021
    Posts:
    40
    I'm really a very newbie to all of this programming stuff. You could tell me if I have a fault.
    upload_2021-7-19_17-50-50.png
     

    Attached Files:

  22. alfonsojoseguerrero

    alfonsojoseguerrero

    Joined:
    Jun 30, 2021
    Posts:
    40
  23. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    Its okay, we were all newbies at one point. I combined the anim.SetBool with the rb2d.velocity line because they are using the same If condition. What you did wasnt technically wrong, but its not clean code.
     
  24. Epsilon_Delta

    Epsilon_Delta

    Joined:
    Mar 14, 2018
    Posts:
    258
    Btw have you checked the white space character? Another thing that is worth trying: hit the ctrl+s with Animator selected - maybe it needs to be serialized.
     
  25. alfonsojoseguerrero

    alfonsojoseguerrero

    Joined:
    Jun 30, 2021
    Posts:
    40
    I have this error.
    upload_2021-7-19_18-1-1.png
     

    Attached Files:

  26. alfonsojoseguerrero

    alfonsojoseguerrero

    Joined:
    Jun 30, 2021
    Posts:
    40
    I already solved the error, I had made a mistake in one thing when writing the code. Anyway the animation still does not work.
     
  27. alfonsojoseguerrero

    alfonsojoseguerrero

    Joined:
    Jun 30, 2021
    Posts:
    40
  28. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    Okay. Run the scene, have the animator window open at the same time, and manually change the bool in the parameters tab. Does the animation work that way?

    If it does, verify the spelling of the bool, like Epsilon_Delta mentioned. You may have a space before or after "istouching" in the parameters tab of the animator window and it has to be exact. One way of figuring this out is to double click or click to change the name of the parameter and then just copy-paste what is there to your code. You could also create another bool parameter and try that one. But you have to have everything exact, no extra spaces or capital letters, etc.
     
  29. alfonsojoseguerrero

    alfonsojoseguerrero

    Joined:
    Jun 30, 2021
    Posts:
    40
    I made some changes to the code and now everything works correctly for me. Thank you very much, without your help I would not have succeeded;)
    upload_2021-7-19_19-14-6.png
     
    Cornysam likes this.
  30. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    Ahhh the player tag was the issue. Glad to hear you figured it out. Nice job
     
  31. alfonsojoseguerrero

    alfonsojoseguerrero

    Joined:
    Jun 30, 2021
    Posts:
    40